JS时间转时间戳,时间戳转时间。时间显示模式。
函数内容
// 时间转为时间戳
function date2timestamp(datetime) {
var timestamp = new Date(Date.parse(datetime));
timestamp = timestamp.getTime();
timestamp = timestamp / 1000;
return timestamp;
}
// 时间戳转时间
function timestamp2date(timestamp, mode) {
var tt = new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ').replace(/年|月/g, "-").replace(/日/g, " ").replace(/上午/g, "").replace(/下午/g, "");
var date_arr = tt.split(" ");
if (mode == 3) {
var minute = 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var current_timestamp = parseInt(Date.parse(new Date()) / 1000);
var diffValue = current_timestamp - timestamp;
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (monthC >= 1) {
result = parseInt(monthC) + "月前";
}
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;
}
if (mode == 2) {
var current_timestamp = parseInt(Date.parse(new Date()) / 1000);
if ((current_timestamp - timestamp) > 7 * 24 * 60 * 60) {
// 一周之前,显示日期
return date_arr[0];
} else {
var d = new Date();
var date = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
var b_date = date2timestamp(date + " 00:00:00");
var e_date = date2timestamp(date + " 23:59:59");
if (parseInt(timestamp) > parseInt(b_date) && parseInt(timestamp) < parseInt(e_date)) {
// 今天,只显示时间
return date_arr[1];
}
if (parseInt(timestamp) > parseInt(b_date - 24 * 60 * 60) && parseInt(timestamp) < parseInt(e_date - 24 * 60 * 60)) {
// 昨天,显示昨天
return "昨天";
}
// 显示周几
var days = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var day = new Date(date_arr[0]).getDay();
return days[day];
}
}
if (mode == 1) {
// 如果是当天,就不显示日期
var d = new Date();
var date = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
var b_date = date2timestamp(date + " 00:00:00");
var e_date = date2timestamp(date + " 23:59:59");
if (parseInt(timestamp) > parseInt(b_date) && parseInt(timestamp) < parseInt(e_date)) {
return date_arr[1];
}
return tt;
}
return tt;
}
// 1498806947 2017.6.30 15:15
// 1498720547 2017.6.29 15:15
// 1498634147 2017.6.28 15:15
// 1497942947 2017.6.20 15:15
console.log(timestamp2date('1498806947', 3));
console.log(timestamp2date('1498720547', 3));
console.log(timestamp2date('1498634147', 3));
console.log(timestamp2date('1497942947', 3));
console.log("-------------------------------");
console.log(timestamp2date('1498806947', 2));
console.log(timestamp2date('1498720547', 2));
console.log(timestamp2date('1498634147', 2));
console.log(timestamp2date('1497942947', 2));
console.log("-------------------------------");
console.log(timestamp2date('1498806947', 1));
console.log(timestamp2date('1498720547', 1));
console.log(timestamp2date('1498634147', 1));
console.log(timestamp2date('1497942947', 1));
console.log("-------------------------------");
console.log(timestamp2date('1498806947'));
console.log(timestamp2date('1498720547'));
console.log(timestamp2date('1498634147'));
console.log(timestamp2date('1497942947'));
执行结果
1小时前
1天前
2天前
1周前
-------------------------------
15:15
昨天
星期三
2017-06-20
-------------------------------
15:15
2017-06-29 15:15
2017-06-28 15:15
2017-06-20 15:15
-------------------------------
2017-06-30 15:15
2017-06-29 15:15
2017-06-28 15:15
2017-06-20 15:15
小结
JS对于时间戳处理不太便捷。需要自己计算处理。根据需要,显示不同的时间模式。