完全に互換性のあるJavaScript時間フォーマット機能、実用的な要約!
コードコピーは次のとおりです。
<!doctype html>
<html>
<head>
<Meta charset = "utf-8" />
<Meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<Title> JS日付フォーマット</title>
<スクリプト言語= "javascript" type = "text/javascript">
/*
*時間のフォーマット
* strdateTime:フォーマットする必要がある文字列時間
* inttype:フォーマットタイプ
*/
function formatdateTime(strdateTime、inttype){
var、月、日、時間、数分、秒。
var newdate、arrdate = new array()、arrime = new array();
試す {
if(strdatetime!= undefined && strdatetime!= null && strdateTime!= ""){
//日付と時刻の配列を取得します
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( ":");
}
//データを処理します
if(arrdate!= undefined && arrime!= undefined
&& arrdate.length == 3 && arrime.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)){
ケース1://形式:yyyy-mm-dd
years = newdate.gettlyear();
月= newdate.getMonth();
if(number(month)<10)month = "0" + month;
days = newdate.getDate();
if(number(days)<10)days = "0" + days;
newdate = years + " - " + month + " - " + days;
壊す;
ケース2://形式:mm-dd HH:mm
月= 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 + ":" +分;
壊す;
ケース3://形式:HH:MM:SS
hours = newdate.gethours();
if(number(hours)<10)hours = "0" + hours;
minutes = newdate.getMinutes();
if(number(minutes)<10)minutes = "0" + minutes;
秒= newdate.getSeconds();
if(number(秒)<10)秒= "0" +秒;
newdate = hours + ":" + minutes + ":" +秒;
壊す;
ケース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;
壊す;
ケース5://形式:yyyy-mm-dd hh:mm
years = newdate.gettlyear();
月= 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 + " - " +月 + " - " + days +
"" + hours + ":" +分;
壊す;
ケース6://形式:yyyy/mm/dd
years = newdate.gettlyear();
月= newdate.getMonth();
if(number(month)<10)month = "0" + month;
days = newdate.getDate();
if(number(days)<10)days = "0" + days;
newdate = years + "/" + month + "/" + days;
壊す;
ケース7://形式:mm/dd HH:mm
月= 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 + ":" +分;
壊す;
ケース8://形式:yyyy/mm/dd HH:mm
years = newdate.gettlyear();
月= 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 + ":" +分;
壊す;
ケース9://形式:yy-mm-dd
years = newdate.gettlyear();
years = years.tostring()。substr(2、2);
月= newdate.getMonth();
if(number(month)<10)month = "0" + month;
days = newdate.getDate();
if(number(days)<10)days = "0" + days;
newdate = years + " - " + month + " - " + days;
壊す;
ケース10://形式:yy/mm/dd
years = newdate.gettlyear();
years = years.tostring()。substr(2、2);
月= newdate.getMonth();
if(number(month)<10)month = "0" + month;
days = newdate.getDate();
if(number(days)<10)days = "0" + days;
newdate = years + "/" + month + "/" + days;
壊す;
ケース11://形式:yyyy year mm month dd hh時間mm分
years = newdate.gettlyear();
月= 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 + "year" + month + "month" + days +
"" + hours + "time" + minutes + "minutes";
壊す;
}
}
}
} catch(e){
newdate = new date();
newdate.getTullyear() + " - " +を返します
(newdate.getMonth() + 1) + " - " +
newdate.getDate() + "" +
newdate.gethours() + ":" +
newdate.getMinutes() + ":" +
newdate.getSeconds();
}
Newdateを返します。
}
</script>
</head>
<body>
<スクリプト言語= "javascript" type = "text/javascript">
//電話
document.writeln(formatdatetime( "2014/04/16 22:34:45"、11));
</script>
</body>
</html>