//app.js小程序公共入口文件
App({
// 自定义方法
puclicFun:function(){
return "公共function"
}
})
//调用公共js对象以便调用其方法
var app = getApp();//获取应用实例
// console.log(app) 可查看公共js(app.js)的方法
Page({
data: {
"data":app.puclicFun()//调用公共appjs的方法
}
})
//时间格式转换(刚刚|几分钟前|几小时前|几周前|几天前|几个月前|具体时间|)
getDateDiff: function (dateTimeStamp) {
var dateTimeStamp = parseInt(dateTimeStamp + '000');
// var dateTimeStamp = parseInt(dateTimeStamp);
var result;
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime();
var diffValue = now - dateTimeStamp;
if(diffValue < 0) {
return;
}
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if(monthC >= 1) {
if (monthC <= 12)
result = "" + parseInt(monthC) + "月前";
else {
result = format(dateTimeStamp) //"" + parseInt(monthC / 12) + "年前";
}
}
else if (weekC >= 1) {
result = "" + parseInt(weekC) + "周前";
}
else if (dayC >= 1) {
result = "" + parseInt(dayC) + "天前";
}
else if (hourC >= 1) {
result = "" + parseInt(hourC) + "小时前";
}
else if (minC >= 1) {
result = "" + parseInt(minC) + "分钟前";
} else {
result = "刚刚";
}
return result;
}
})
function add0(m) { return m < 10 ? '0' + m : m }
function format(shijianchuo) {
//shijianchuo是整数,否则要parseInt转换
var time = new Date(shijianchuo);
var y = time.getFullYear();
var m = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}