JS中日期格式化的方法
原创
©著作权归作者所有:来自51CTO博客作者swh0318的原创作品,请联系作者获取转载授权,否则将追究法律责任
- 将格式为2016-09-09 12:00:00转化为 2016-09-08 12:00:00,将固定时间格式减一天转化方法,
通过字符串拼接的方式进行组合,可以模仿进行转化。
var endTime = '2016-09-09 12:00:00';
var date = new Date(Date.parse(endTime.replace(/-/g, "/")));
var dateStartTime = date.getTime()-24*3600*1000;
var d=new Date(dateStartTime);
var seperator1 = "-";
var seperator2 = ":";
var month = d.getMonth() + 1;
var strDate = d.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var strHour = d.getHours();
var strMinute = d.getMinutes()-1;
if (strHour >= 0 && strHour <= 9) {
strHour = "0" + strHour;
}
if (strMinute >= 0 && strMinute <= 9) {
strMinute = "0" + strMinute;
}
startTime = d.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + strHour + seperator2 + strMinute
+ seperator2 + '00';
结果: startTime = '2016-09-08 12:00:00';