JavaScriptの時刻書式設定機能完全対応、実践まとめ! コードをコピー コードは次のとおりです: equiv="Content-Type" content="text/html; charset=utf-8" /> js 日付形式 <br>/* <br>* 時刻の書式設定<br>* strDateTime: 書式設定が必要な文字列時刻<br>* intType: 書式設定の種類<br>*/ <br>function formatDateTime( strDateTime, intType) { <br>var 年、月、日、時、分、秒; <br>var newDate, arrDate = new Array(), arrTime = new Array(); try { <br>if (strDateTime != unknown && 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 = years "年" month "月" days <BR>" " 時 " 分 "分"; <BR>休憩; <BR>} <BR>} <BR>} <BR>} catch (e) { <BR>newDate = new Date(); <br><br><BR>return newDate.getFull Year() "-" <BR>(newDate.getMonth() 1) "-" <BR>newDate.getDate() " " <BR>newDate.getHours() ":" <BR>newDate.getMinutes() ":" <BR>newDate.getSeconds(); <BR>} <br><br><BR>newDate を返します。 <BR>} <BR> <br>//调用 <br>document.writeln(formatDateTime("2014/04/16 22:34:45", 11)); <br>