완벽하게 호환되는 자바스크립트 시간 형식 지정 기능, 실용적인 요약! 코드 복사 코드는 다음과 같습니다. js 날짜 형식 지정 <br>/* <br>* 시간 형식<br>* strDateTime: 형식을 지정해야 하는 문자열 시간<br>* intType: 형식 형식<br>*/ <br>함수 formatDateTime( strDateTime, intType) { <br>var 연, 월, 일, 시간, 분, 초 <br>var newDate, arrDate = new Array(), arrTime = new Array(); try { <br>if (strDateTime != 정의되지 않음 && 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(" "); item[0].toString().split("/"); <br>arrTime = item[1].toString().split(":"); <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 (수(분) < 10) 분 = "0"분; <br><br><BR>newDate = 년 "年" 월 "月" 일 <BR>" " 시간 "时" 분 "分"; <BR>휴식; <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>