码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-19 12:29:08      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:minutes   eth   else   连接   indexof   var   日期格   date   简单   

 

 

//时间格式转换
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;
}

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

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

标签:minutes   eth   else   连接   indexof   var   日期格   date   简单   

原文地址:http://www.cnblogs.com/liucldq/p/8063843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!