js將日期格式轉換為YYYY-MM-DD HH:MM:SS
更新時間:2020年09月18日 11:48:33 作者:別先生
這篇文章主要介紹了js將日期格式轉換為YYYY-MM-DD HH:MM:SS,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
1、百度不少js將日期格式轉換為YYYY-MM-DD HH:MM:SS ??墒嵌悸燥@復雜,所以這里總結了一下,自己找到的,方便自己學習和使用。
方法一:
項目源碼:
$("#createTime").text((new Date(jumpParams.createDate.time).Format("yyyy-MM-dd hh:mm:ss")));
$("#updateTime").text((new Date(jumpParams.updateDate.time).Format("yyyy-MM-dd hh:mm:ss")));
關鍵點:
xxx.Format("yyyy-MM-dd hh:mm:ss");調用這句話就可以將Sun May 27 2018 11:08:09 GMT+0800 (中國標準時間)格式的時間轉換為"2018-05-27 11:08:09"格式的時間。
方法二:
項目源碼:
$("#createTime").text((ChangeDateFormat(new Date(jumpParams.createDate.time))));
$("#updateTime").text((ChangeDateFormat(new Date(jumpParams.updateDate.time))));
封裝方法調用:
function ChangeDateFormat(date) {
return date.Format("yyyy-MM-dd hh:mm:ss");
}
關鍵點:
注意括號和自己的時間格式即可。
可以使用瀏覽器工具,對轉換進行查看:

其他方法
function formatDate(date,cut) {
var date = new Date(date);
var YY = date.getFullYear() + cut;
var MM =
(date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1) + cut;
var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hh =
(date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
var mm =
(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
":";
var ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return YY + MM + DD + " " + hh + mm + ss;
}
正則方法
function farmatDate(time, fmt) {
if (/(y+)/.test(fmt) {
fmt = fmt.replace(RegExp.$1, date.getFullYear() + '').substr(4 - RegExp.$1.length);
}
let o = {
'M+': getMonth() + 1,
'd+': getDay(),
'h+': getHours(),
'm+': getMinutes(),
's+': getSeconds()
};
for (let key in o) {
if(RegExp(`(${key})`.test(fmt)) {
let str = o[key] + '';
fmt = fmt.replace(RegExp.$1, str.length === 2 ? str:padLeftZero(str);
}
}
return fmt;
}
// 函數(shù) padLeftZero 的作用:如果月份為1位(如9),則在其左邊補0(變?yōu)?9)
function padLeftZero(str) {
return '00' + substr(str.length);
}
// 舉例
let res = formatDate('1469261964000', 'yyyy-MM-dd hh:mm');
console.log(res); // 2016-07-06 16:19
到此這篇關于js將日期格式轉換為YYYY-MM-DD HH:MM:SS的文章就介紹到這了,更多相關js YYYY-MM-DD HH:MM:SS內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript實現(xiàn)二級級聯(lián)菜單的簡單制作
這篇文章主要介紹了javascript實現(xiàn)二級級聯(lián)菜單的簡單制作,感興趣的小伙伴們可以參考一下2015-11-11
JavaScript遍歷json對象數(shù)據(jù)的方法
這篇文章介紹了JavaScript遍歷json對象數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

