全面相容的javascript時間格式化函數,實用總結! 複製程式碼 程式碼如下: js日期格式化 <BR>/* <BR>* 時間格式化<BR>* strDateTime:需要格式化的字串時間<BR>* intType:格式化類型<BR>*/ <BR>function formatDateTime(strDateTime, intType) { <BR>var years, month, days, hours, minutes, seconds; <BR>var newDate, arrDate = new Array(), arrTime = new Array(); <br><br><BR>try { <BR>if strDateTime != undefined && strDateTime != null && strDateTime != "") { <BR>//取得日期與時間陣列<BR>if (strDateTime.indexOf("-") != -1) { <BR>var item = strDateTime.split(" "); <BR>arrDate = item[0].toString().split("-"); <BR>arrTime = item[1].toString().split(":" ); <BR>} else if (strDateTime.indexOf("/") != -1) { <BR>var item = strDateTime.split(" "); <BR>arrDate = item[0].toString() .split("/"); <BR>arrTime = item[1].toString().split(":"); <BR>} <br><br><BR>//处理数据 <BR>if (arrDate != undefined && arrTime != undefined <BR>&& arrDate.length == 3 && arrTime.length == 3) { <BR>newDate = new Date( <BR>parseInt(arrDate[0]), <BR>parseInt(arrDate[1]), <BR>parseInt(arrDate[2]), <BR>parseInt(arrTime[0]), <BR>parseInt(arrTime[1]), <BR>parseInt(arrTime[2]) <BR>); <br><br><BR>switch (Number(intType)) { <BR>case 1: //格式:yyyy-MM-dd <BR>years = newDate.getFullYear(); <br><br><BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>newDate = years + "-" + month + "-" + days; <BR>break; <BR>case 2: //格式:MM-dd HH:mm <BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>hours = newDate.getHours(); <BR>if (Number(hours) < 10) hours = "0" + hours; <br><br><BR>minutes = newDate.getMinutes(); <BR>if (Number(minutes) < 10) minutes = "0" + minutes; <br><br><BR>newDate = month + "-" + days + <BR>" " + hours + ":" + minutes; <BR>break; <BR>case 3: //格式:HH:mm:ss <BR>hours = newDate.getHours(); <BR>if (Number(hours) < 10) hours = "0" + hours; <br><br><BR>minutes = newDate.getMinutes(); <BR>if (Number(minutes) < 10) minutes = "0" + minutes; <br><br><BR>seconds = newDate.getSeconds(); <BR>if (Number(seconds) < 10) seconds = "0" + seconds; <br><br><BR>newDate = hours + ":" + minutes + ":" + seconds; <BR>break; <BR>case 4: //格式:HH:mm <BR>hours = newDate.getHours(); <BR>if (Number(hours) < 10) hours = "0" + hours; <br><br><BR>minutes = newDate.getMinutes(); <BR>if (Number(minutes) < 10) minutes = "0" + minutes; <br><br><BR>newDate = hours + ":" + minutes; <BR>break; <BR>case 5: //格式:yyyy-MM-dd HH:mm <BR>years = newDate.getFullYear(); <br><br><BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>hours = newDate.getHours(); <BR>if (Number(hours) < 10) hours = "0" + hours; <br><br><BR>minutes = newDate.getMinutes(); <BR>if (Number(minutes) < 10) minutes = "0" + minutes; <br><br><BR>newDate = years + "-" + month + "-" + days + <BR>" " + hours + ":" + minutes; <BR>break; <BR>case 6: //格式:yyyy/MM/dd <BR>years = newDate.getFullYear(); <br><br><BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>newDate = years + "/" + month + "/" + days; <BR>break; <BR>case 7: //格式:MM/dd HH:mm <BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>hours = newDate.getHours(); <BR>if (Number(hours) < 10) hours = "0" + hours; <br><br><BR>minutes = newDate.getMinutes(); <BR>if (Number(minutes) < 10) minutes = "0" + minutes; <br><br><BR>newDate = month + "/" + days + <BR>" " + hours + ":" + minutes; <BR>break; <BR>case 8: //格式:yyyy/MM/dd HH:mm <BR>years = newDate.getFullYear(); <br><br><BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>hours = newDate.getHours(); <BR>if (Number(hours) < 10) hours = "0" + hours; <br><br><BR>minutes = newDate.getMinutes(); <BR>if (Number(minutes) < 10) minutes = "0" + minutes; <br><br><BR>newDate = years + "/" + month + "/" + days + <BR>" " + hours + ":" + minutes; <BR>break; <BR>case 9: //格式:yy-MM-dd <BR>years = newDate.getFullYear(); <BR>years = years.toString().substr(2, 2); <br><br><BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>newDate = years + "-" + month + "-" + days; <BR>break; <BR>case 10: //格式:yy/MM/dd <BR>years = newDate.getFullYear(); <BR>years = years.toString().substr(2, 2); <br><br><BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>newDate = years + "/" + month + "/" + days; <BR>break; <BR>case 11: //格式:yyyy年MM月dd hh时mm分 <BR>years = newDate.getFullYear(); <br><br><BR>month = newDate.getMonth(); <BR>if (Number(month) < 10) month = "0" + month; <br><br><BR>days = newDate.getDate(); <BR>if (Number(days) < 10) days = "0" + days; <br><br><BR>hours = newDate.getHours(); <BR>if (Number(hours) < 10) hours = "0" + hours; <br><br><BR>minutes = newDate.getMinutes(); <BR>if (Number(分鐘) < 10) 分鐘= "0" 分鐘; <br><br><BR>newDate = 年"年" 月"月" 天<BR>" " 小時"時" 分鐘"分"; <BR>休息; <BR>} <BR>} <BR>} <BR>} catch (e) { <BR>newDate = new Date(); <br><br><BR>return newDate(); <BR><BR><BR>return newDate.getFullY(ate. ) "-" <BR>(newDate.getMonth() 1) "-" <BR>newDate.getDate() " " <BR>newDate.getHours() ":" <br>newDate.getMinutes() ":" <br>newDate.getSeconds(); <BR>} <BR><BR><BR>回新日期;<BR>} <BR></腳本> <BR></頭> <BR><身體>> <BR><腳本語言=“javascript”類型=“text/javascript”> <BR>//呼叫<BR>document.writeln(formatDateTime("2014/04/16 22:34:45", 11)); <BR></腳本> 身體>