/** * @param {数值类型}date */ functionformatDate(date){ date = newDate(date); var y = date.getFullYear(); var m = date.getMonth()+1; var d = date.getDate(); var h = date.getHours(); var m1 = date.getMinutes(); var s = date.getSeconds(); m = m<10?("0"+m):m; d = d<10?("0"+d):d; return y+"-"+m+"-"+d+" "+h+":"+m1+":"+s; } // 例 console.log(formatDate(1521783347000)); // 2018-03-23 13:35:47 参数为Number
二、将标准时间格式转换成毫秒
1 2
var date = Date.parse(newDate('2018-03-23 13:35:47'));// 参数为String console.log(date); // 1521783347000 单位:毫秒
三、格式化Thu May 12 2017 08:00:00 GMT+0800 (中国标准时间) 转字符串
1 2 3 4 5 6 7 8 9
functioncheckTime(i){ if (i < 10){ i = "0" + i } return i; } var date = newDate('Thu May 12 2020 08:00:00 GMT+0800 (中国标准时间)'); let date_value = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + checkTime(date.getHours()) + ':' + checkTime(date.getMinutes()) + ':' + checkTime(date.getSeconds()); console.log(date_value) // 最后显示yyyy-MM-dd hh:mm:ss 2020-5-12 08:00:00
四、字符串Wed Dec 13 2017 16:00:00 GMT+0800 (中国标准时间)
1 2 3 4 5 6 7 8 9 10 11
// 如何将2017-12-13 16:00:00转换成标准时间? functionstr_time(date) { var t = Date.parse(date); if (!isNaN(t)) { returnnewDate(Date.parse(date.replace(/-/g, "/"))); } else{ returnnewDate(); } } console.log(str_time('2017-12-13 16:00:0')) // 输入为:Wed Dec 13 2017 16:00:00 GMT+0800 (中国标准时间)