console.log(new Date()) //Mon Oct 08 2018 14:25:59 GMT+0800 (中国标准时间)
console.log(new Date(22222)) //Thu Jan 01 1970 08:00:22 GMT+0800 (中国标准时间)
1-1.标准时间格式转换成yyyy-MM-dd的格式的日期 方法1
Date.prototype.Format = function(fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
转换实例
console.log((new Date(22222)).Format('yyyy-MM-dd '))//1970-01-01
console.log((new Date(22222)).Format('yyyy-MM-dd hh:mm:ss'))//1970-01-01 08:00:22
console.log((new Date(22222)).Format('yyyy.MM.dd'))//1970.01.01
1-2.转换成yyyy-MM-dd的格式的日期 方法2
function transferTime(dateStr){
var date = new Date(dateStr);
var Month = date.getMonth() + 1;
var Day = date.getDate();
var Y = date.getFullYear() + '-';
var M = Month < 10 ? '0' + Month + '-' : Month + '-';
var D = Day + 1 < 10 ? '0' + Day : Day;
return Y + M + D;
}
转换实例
console.log(transferTime('2018-10-24 14:36:00')) //2018-10-24
console.log(transferTime(22222)) //1970-01-01
2.yyyy-MM-dd的格式 转换成标准时间格式的日期
var parserDate = function (date) {
var t = Date.parse(date);
if (!isNaN(t)) {
return new Date(Date.parse(date.replace(/-/g, "/")));
} else {
return new Date();
}
};
转换实例
console.log(parserDate("2018-10-08")) //Mon Oct 08 2018 00:00:00 GMT+0800 (中国标准时间)
console.log(parserDate("2018.11.08 14:36:00"))//Thu Nov 08 2018 14:36:00 GMT+0800 (中国标准时间)
console.log(parserDate("2018.11.08")) //Thu Nov 08 2018 00:00:00 GMT+0800 (中国标准时间)
网友评论