国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

全面兼容的javascript時(shí)間格式化函數(shù)(比較實(shí)用)

2019-11-20 15:26:50
字體:
供稿:網(wǎng)友
全面兼容的javascript時(shí)間格式化函數(shù),實(shí)用總結(jié)!
復(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>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 翼城县| 栾城县| 雷山县| 张家界市| 辉县市| 龙南县| 临猗县| 秦安县| 广西| 读书| 平昌县| 桦南县| 肥城市| 闽侯县| 衡山县| 三门峡市| 汉中市| 康马县| 旺苍县| 栾川县| 团风县| 卢龙县| 类乌齐县| 黑龙江省| 饶阳县| 登封市| 万盛区| 武强县| 桐城市| 安福县| 凤凰县| 小金县| 旬阳县| 海原县| 正定县| 汤阴县| 中江县| 资源县| 固原市| 北安市| 集安市|