简单的时间日期格式化(未封装成控件)

 

 

//时间格式转换
function processDateTimeShow(dateTime) {
if (!dateTime) return "时间不详";
if (dateTime.indexOf("T") > 0) {
dateTime = dateTime.replace('T', ' ');
}
dateTime = dateTime.replace(/-/g, '/');//正则表达式过滤

var showTime = dateTime;
var today = new Date();
var curYear = today.getFullYear();
var curMonth = today.getMonth() + 1;
var curDate = today.getDate();

var mdate = new Date(dateTime);
var msgYear = mdate.getFullYear();
var msgMonth = mdate.getMonth() + 1;
var msgDate = mdate.getDate();

var msgMin = mdate.getMinutes();
if (msgMin < 10) {
msgMin = "0" + msgMin;
}

if (curYear === msgYear) {
if (curMonth === msgMonth && curDate === msgDate) {
showTime = mdate.getHours() + ":" + msgMin;
}
else {
var t = today.getTime() - 1000 * 60 * 60 * 24;
var yesterday = new Date(t);
var yesMonth = yesterday.getMonth() + 1;
var yesDate = yesterday.getDate();
if (curYear === msgYear && curMonth === yesMonth && msgDate === yesDate) {
showTime = "昨天 " + mdate.getHours() + ":" + msgMin;
}
else {
showTime = msgMonth + "-" + msgDate + " " + mdate.getHours() + ":" + msgMin;
}
}
}
else {
showTime = msgYear + "-" + msgMonth + "-" + msgDate + " " + mdate.getHours() + ":" + msgMin;
}
return showTime;
}

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2017-12-19 09:14  弒晓风  阅读(344)  评论(0编辑  收藏  举报