全面兼容的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(minutes) < 10) minutes = "0" + minutes; <br><br><BR>newDate = years + "年" + month + "月" + days + <BR>" " + hours + "时" + minutes + "分"; <BR>break; <BR>} <BR>} <BR>} <BR>} catch (e) { <BR>newDate = new Date(); <br><br><BR>return newDate.getFullYear() + "-" + <BR>(newDate.getMonth() + 1) + "-" + <BR>newDate.getDate() + " " + <BR>newDate.getHours() + ":" + <BR>newDate.getMinutes() + ":" + <BR>newDate.getSeconds(); <BR>} <br><br><BR>return newDate; <BR>} <BR> <BR>//调用 <BR>document.writeln(formatDateTime("2014/04/16 22:34:45", 11)); <BR>