隐藏

json 返回数据时间的绑定的转换 几天前几小时前格式

发布:2023/12/7 15:12:16作者:大数据 来源:大数据 浏览次数:421

json 返回数据时间的绑定的转换

 

function ChangeDateFormat(dateTime) {
var date = new Date(parseInt(dateTime.replace("/Date(", "").replace(")/", ""), 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var min = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
// var sec = date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds();
//return date.getFullYear() + "-" + month + "-" + currentDate + " " +hour+":"+min+":"+sec;
return date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + min;
}

 

直接调用就行!

 

 

function GetDateDiff(dateTime) {
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var month = day * 30;

var tempTime = dateTime ? Date.parse(dateTime.replace(/-/gi, "/")) : new Date().getTime();

var now = new Date().getTime();
var diffValue = now - tempTime;

var monthTemp = diffValue / month;
var weekTemp= diffValue / (7 * day);
var dayTemp = diffValue / day;
var hourTemp = diffValue / hour;
var minTemp = diffValue / minute;
if (monthTemp >= 1) {
return dateTime;
}
else if (weekTemp >= 1) {
result = "" + parseInt(weekTemp) + " 个星期前";
}
else if (dayTemp >= 1) {
result = "" + parseInt(dayTemp) + " 天前";
}
else if (hourTemp >= 1) {
result = "" + parseInt(hourTemp) + " 个小时前";
}
else if (minTemp >= 1) {
result = "" + parseInt(minTemp) + " 分钟前";
} else
result = "刚刚发表";
return result;
}

声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!