全面兼容的javascript時(shí)間格式化函數(shù)(比較實(shí)用)
更新時(shí)間:2014年05月14日 09:28:14 作者:
這篇文章主要介紹了全面兼容比較實(shí)用的javascript時(shí)間格式化函數(shù),需要的朋友可以參考下
全面兼容的javascript時(shí)間格式化函數(shù),實(shí)用總結(jié)!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js日期格式化</title>
<script language="javascript" type="text/javascript">
/*
* 時(shí)間格式化
* strDateTime:需要格式化的字符串時(shí)間
* intType:格式化類型
*/
function formatDateTime(strDateTime, intType) {
var years, month, days, hours, minutes, seconds;
var newDate, arrDate = new Array(), arrTime = new Array();
try {
if (strDateTime != undefined && strDateTime != null && strDateTime != "") {
//獲取日期和時(shí)間數(shù)組
if (strDateTime.indexOf("-") != -1) {
var item = strDateTime.split(" ");
arrDate = item[0].toString().split("-");
arrTime = item[1].toString().split(":");
} else if (strDateTime.indexOf("/") != -1) {
var item = strDateTime.split(" ");
arrDate = item[0].toString().split("/");
arrTime = item[1].toString().split(":");
}
//處理數(shù)據(jù)
if (arrDate != undefined && arrTime != undefined
&& arrDate.length == 3 && arrTime.length == 3) {
newDate = new Date(
parseInt(arrDate[0]),
parseInt(arrDate[1]),
parseInt(arrDate[2]),
parseInt(arrTime[0]),
parseInt(arrTime[1]),
parseInt(arrTime[2])
);
switch (Number(intType)) {
case 1: //格式:yyyy-MM-dd
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "-" + month + "-" + days;
break;
case 2: //格式:MM-dd HH:mm
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = month + "-" + days +
" " + hours + ":" + minutes;
break;
case 3: //格式:HH:mm:ss
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
seconds = newDate.getSeconds();
if (Number(seconds) < 10) seconds = "0" + seconds;
newDate = hours + ":" + minutes + ":" + seconds;
break;
case 4: //格式:HH:mm
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = hours + ":" + minutes;
break;
case 5: //格式:yyyy-MM-dd HH:mm
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "-" + month + "-" + days +
" " + hours + ":" + minutes;
break;
case 6: //格式:yyyy/MM/dd
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "/" + month + "/" + days;
break;
case 7: //格式:MM/dd HH:mm
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = month + "/" + days +
" " + hours + ":" + minutes;
break;
case 8: //格式:yyyy/MM/dd HH:mm
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "/" + month + "/" + days +
" " + hours + ":" + minutes;
break;
case 9: //格式:yy-MM-dd
years = newDate.getFullYear();
years = years.toString().substr(2, 2);
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "-" + month + "-" + days;
break;
case 10: //格式:yy/MM/dd
years = newDate.getFullYear();
years = years.toString().substr(2, 2);
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "/" + month + "/" + days;
break;
case 11: //格式:yyyy年MM月dd hh時(shí)mm分
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "年" + month + "月" + days +
" " + hours + "時(shí)" + minutes + "分";
break;
}
}
}
} catch (e) {
newDate = new Date();
return newDate.getFullYear() + "-" +
(newDate.getMonth() + 1) + "-" +
newDate.getDate() + " " +
newDate.getHours() + ":" +
newDate.getMinutes() + ":" +
newDate.getSeconds();
}
return newDate;
}
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
//調(diào)用
document.writeln(formatDateTime("2014/04/16 22:34:45", 11));
</script>
</body>
</html>
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js日期格式化</title>
<script language="javascript" type="text/javascript">
/*
* 時(shí)間格式化
* strDateTime:需要格式化的字符串時(shí)間
* intType:格式化類型
*/
function formatDateTime(strDateTime, intType) {
var years, month, days, hours, minutes, seconds;
var newDate, arrDate = new Array(), arrTime = new Array();
try {
if (strDateTime != undefined && strDateTime != null && strDateTime != "") {
//獲取日期和時(shí)間數(shù)組
if (strDateTime.indexOf("-") != -1) {
var item = strDateTime.split(" ");
arrDate = item[0].toString().split("-");
arrTime = item[1].toString().split(":");
} else if (strDateTime.indexOf("/") != -1) {
var item = strDateTime.split(" ");
arrDate = item[0].toString().split("/");
arrTime = item[1].toString().split(":");
}
//處理數(shù)據(jù)
if (arrDate != undefined && arrTime != undefined
&& arrDate.length == 3 && arrTime.length == 3) {
newDate = new Date(
parseInt(arrDate[0]),
parseInt(arrDate[1]),
parseInt(arrDate[2]),
parseInt(arrTime[0]),
parseInt(arrTime[1]),
parseInt(arrTime[2])
);
switch (Number(intType)) {
case 1: //格式:yyyy-MM-dd
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "-" + month + "-" + days;
break;
case 2: //格式:MM-dd HH:mm
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = month + "-" + days +
" " + hours + ":" + minutes;
break;
case 3: //格式:HH:mm:ss
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
seconds = newDate.getSeconds();
if (Number(seconds) < 10) seconds = "0" + seconds;
newDate = hours + ":" + minutes + ":" + seconds;
break;
case 4: //格式:HH:mm
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = hours + ":" + minutes;
break;
case 5: //格式:yyyy-MM-dd HH:mm
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "-" + month + "-" + days +
" " + hours + ":" + minutes;
break;
case 6: //格式:yyyy/MM/dd
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "/" + month + "/" + days;
break;
case 7: //格式:MM/dd HH:mm
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = month + "/" + days +
" " + hours + ":" + minutes;
break;
case 8: //格式:yyyy/MM/dd HH:mm
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "/" + month + "/" + days +
" " + hours + ":" + minutes;
break;
case 9: //格式:yy-MM-dd
years = newDate.getFullYear();
years = years.toString().substr(2, 2);
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "-" + month + "-" + days;
break;
case 10: //格式:yy/MM/dd
years = newDate.getFullYear();
years = years.toString().substr(2, 2);
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
newDate = years + "/" + month + "/" + days;
break;
case 11: //格式:yyyy年MM月dd hh時(shí)mm分
years = newDate.getFullYear();
month = newDate.getMonth();
if (Number(month) < 10) month = "0" + month;
days = newDate.getDate();
if (Number(days) < 10) days = "0" + days;
hours = newDate.getHours();
if (Number(hours) < 10) hours = "0" + hours;
minutes = newDate.getMinutes();
if (Number(minutes) < 10) minutes = "0" + minutes;
newDate = years + "年" + month + "月" + days +
" " + hours + "時(shí)" + minutes + "分";
break;
}
}
}
} catch (e) {
newDate = new Date();
return newDate.getFullYear() + "-" +
(newDate.getMonth() + 1) + "-" +
newDate.getDate() + " " +
newDate.getHours() + ":" +
newDate.getMinutes() + ":" +
newDate.getSeconds();
}
return newDate;
}
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
//調(diào)用
document.writeln(formatDateTime("2014/04/16 22:34:45", 11));
</script>
</body>
</html>
您可能感興趣的文章:
相關(guān)文章
微信小程序?qū)崿F(xiàn)左側(cè)滑動(dòng)導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)左側(cè)滑動(dòng)導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10微信小程序?qū)崿F(xiàn)日期格式化和倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)日期格式化和倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06JavaScript實(shí)現(xiàn)的九種排序算法
這篇文章主要給大家介紹了關(guān)于利用JavaScript實(shí)現(xiàn)的九種排序算法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03javascript設(shè)計(jì)模式 – 迭代器模式原理與用法實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 迭代器模式原理與用法,結(jié)合實(shí)例形式分析了javascript迭代器模式相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04E3 tree 1.6在Firefox下顯示問題的修復(fù)方法
tree 在Firefox下只顯示一句話,用firebug查看頁面元素觀察發(fā)現(xiàn),兩個(gè)script導(dǎo)入被一個(gè)<script>分隔開了,顯然是document.write的問題.由于Firefox對(duì)js規(guī)范的檢查比較嚴(yán)格,肯定一些字符輸出的的時(shí)候沒有轉(zhuǎn)義2013-01-01javascript實(shí)現(xiàn)的制作特殊字的腳本
javascript實(shí)現(xiàn)的制作特殊字的腳本...2007-06-06JS首屏加載時(shí)間優(yōu)化的解決方法總結(jié)
首屏加載時(shí)間是一個(gè)衡量網(wǎng)頁性能和用戶體驗(yàn)的關(guān)鍵指標(biāo),這個(gè)問題無論是在面試中還是在項(xiàng)目開發(fā)中都占有極其高的權(quán)重,本文為大家整理了幾種JS中優(yōu)化首屏加載時(shí)間的方法,希望對(duì)大家有所幫助2024-02-02