var util = require('../../utils/util.js');
const app = getApp()
Page({
data: {
aftertime:'',
timeD:''
},
onLoad: function (options) {
var stringTime = "2018-08-09 17:40:00";
var stringTime = new Date(Date.parse(stringTime.replace(/-/g, "/")));
var date = stringTime.getTime();
console.log(date)
// 调用函数时,传入new Date()参数,返回值是日期和时间
var now = util.formatTime(new Date());
if (stringTime !== now){
this.setData({
timeD: dateDiff(date)
})
}
if (stringTime !== now){
this.setData({
aftertime: stringTime.getMonth() + 1 + '/' + stringTime.getDate()
})
}else{
aftertime:stringTime.getMonth() +1 + '/' + stringTime.getDate()
}
},
onShow: function () {
}
})
var dateDiff = function (timestamp) {
// 补全为13位
var arrTimestamp = (timestamp + '').split('');
for (var start = 0; start < 13; start++) {
if (!arrTimestamp[start]) {
arrTimestamp[start] = '0';
}
}
timestamp = arrTimestamp.join('') * 1;
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 - timestamp;
// 如果本地时间反而小于变量时间
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;
// 数值补0方法
var zero = function (value) {
if (value < 10) {
return '0' + value;
}
return value;
};
// 使用
if (monthC > 12) {
// 超过1年,直接显示年月日
return (function () {
var date = new Date(timestamp);
return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
})();
} else if (monthC >= 1) {
return parseInt(monthC) + "月前";
} else if (weekC >= 1) {
return parseInt(weekC) + "周前";
} else if (dayC >= 1) {
return parseInt(dayC) + "天前";
} else if (hourC >= 1) {
return parseInt(hourC) + "小时前";
} else if (minC >= 1) {
return parseInt(minC) + "分钟前";
}
<view class="v_beforetime">1:{{timeD}}</view>
<view class="v_aftertime">2:{{aftertime}}</view>
return '刚刚';
};
第二:
var now = new Date().getFullYear()+'/'+new Date().getMonth()+'/'+new Date().getDate();
var uptime = new Date(res.data.data.update_time.replace(/-/g,'/'));
console.log(now,uptime.getDate())
if((uptime.getFullYear()+'/'+uptime.getMonth()+'/'+uptime.getDate()) !== now){
res.data.data.update_time = uptime.getMonth()+1 + '月' + uptime.getDate() + '日'
}else{
res.data.data.update_time = uptime.getHours() + ':' + uptime.getMinutes();
}
var now = new Date().getFullYear()+'/'+new Date().getMonth()+'/'+new Date().getDate();
res.data.data.forEach((item,key)=>{
var uptime = new Date(item.update_time*1000);
if((uptime.getFullYear()+'/'+uptime.getMonth()+'/'+uptime.getDate()) !== now){
item.update_time = uptime.getMonth()+1 + '月' + uptime.getDate() + '日'
}else{
item.update_time = uptime.getHours() + ':' + uptime.getMinutes();
}
})