완전히 호환 가능한 JavaScript 시간 서식 기능, 실제 요약!
코드 사본은 다음과 같습니다.
<! doctype html>
<html>
<헤드>
<meta charset = "utf-8" />
<meta http-equiv = "content-type"content = "text /html; charset = utf-8" />
<title> js 날짜 서식 </title>
<script language = "javaScript"type = "text/javaScript">
/*
* 시간 형식
* strdateTime : 형식이 필요한 문자열 시간
* inttype : 형식 유형
*/
fortmatdateTime (strdateTime, inttype) {
var 년, 월, 일, 시간, 몇 분, 초;
var newdate, arrdate = new array (), arrtime = new array ();
노력하다 {
if (strdateTime! = undefined && strdateTime! = null && strdateTime! = "") {
// 날짜와 시간 배열을 얻습니다
if (strdateMe.IndexOf ( " -")! = -1) {
var item = strdateTime.split ( "");
arrdate = item [0] .toString (). split ( "-");
arrtime = item [1] .toString (). split ( ":");
} else if (strdateMe.IndexOf ( "/")! = -1) {
var item = strdateTime.split ( "");
arrdate = item [0] .toString (). split ( "/");
arrtime = item [1] .toString (). split ( ":");
}
// 프로세스 데이터
if (arrdate! = undefined && arrtime! = 정의되지 않았습니다
&& arrdate.length == 3 && arrtime.length == 3) {
NewDate = 새로운 날짜 (
parseint (Arrdate [0]),
parseint (Arrdate [1]),
parseint (Arrdate [2]),
parseint (Arrime [0]),
parseint (Arrime [1]),
parseint (Arrime [2])
);
스위치 (번호 (inttype)) {
사례 1 : // 형식 : yyyy-mm-dd
년 = newDate.getPyear ();
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
NewDate = years + "-" + Month + "-" + Days;
부서지다;
사례 2 : // 형식 : MM-DD HH : MM
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
시간 = newDate.gethours ();
if (number (시간) <10) 시간 = "0" + 시간;
minutes = newDate.getMinutes ();
if (number (memers) <10) ming = "0" + mings;
NewDate = Month + "-" + Days +
"" + 시간 + ":" + 분;
부서지다;
사례 3 : // 형식 : HH : MM : SS
시간 = newDate.gethours ();
if (number (시간) <10) 시간 = "0" + 시간;
minutes = newDate.getMinutes ();
if (number (memers) <10) ming = "0" + mings;
SECONDS = newDate.getSeconds ();
if (number (seconds) <10) 초 = "0" + 초;
NewDate = Hours + ":" + minutes + ":" + 초;
부서지다;
사례 4 : // 형식 : HH : MM
시간 = newDate.gethours ();
if (number (시간) <10) 시간 = "0" + 시간;
minutes = newDate.getMinutes ();
if (number (memers) <10) ming = "0" + mings;
NewDate = Hours + ":" + 분;
부서지다;
사례 5 : // 형식 : yyyy-mm-dd hh : mm
년 = newDate.getPyear ();
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
시간 = newDate.gethours ();
if (number (시간) <10) 시간 = "0" + 시간;
minutes = newDate.getMinutes ();
if (number (memers) <10) ming = "0" + mings;
NewDate = years + "-" + Month + "-" + Days +
"" + 시간 + ":" + 분;
부서지다;
사례 6 : // 형식 : yyyy/mm/dd
년 = newDate.getPyear ();
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
NewDate = 년 + "/" + Month + "/" + 일;
부서지다;
사례 7 : // 형식 : MM/DD HH : MM
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
시간 = newDate.gethours ();
if (number (시간) <10) 시간 = "0" + 시간;
minutes = newDate.getMinutes ();
if (number (memers) <10) ming = "0" + mings;
NewDate = Month + "/" + Days +
"" + 시간 + ":" + 분;
부서지다;
사례 8 : // 형식 : yyyy/mm/dd hh : mm
년 = newDate.getPyear ();
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
시간 = newDate.gethours ();
if (number (시간) <10) 시간 = "0" + 시간;
minutes = newDate.getMinutes ();
if (number (memers) <10) ming = "0" + mings;
NewDate = years + "/" + Month + "/" + Days +
"" + 시간 + ":" + 분;
부서지다;
사례 9 : // 형식 : yy-mm-dd
년 = newDate.getPyear ();
연도 = 년 .TOSTRING (). substr (2, 2);
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
NewDate = years + "-" + Month + "-" + Days;
부서지다;
사례 10 : // 형식 : yy/mm/dd
년 = newDate.getPyear ();
연도 = 년 .TOSTRING (). substr (2, 2);
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
NewDate = 년 + "/" + Month + "/" + 일;
부서지다;
사례 11 : // 형식 : yyyy 연도 mm 달 dd hh 시간 mm 분
년 = newDate.getPyear ();
Month = NewDate.getMonth ();
if (number (month) <10) Month = "0" + Month;
days = newDate.getDate ();
if (number (days) <10) days = "0" + 일;
시간 = newDate.gethours ();
if (number (시간) <10) 시간 = "0" + 시간;
minutes = newDate.getMinutes ();
if (number (memers) <10) ming = "0" + mings;
NewDate = Years + "Year" + Month + "Month" + Days +
"" + 시간 + "시간" + 분 + "분";
부서지다;
}
}
}
} catch (e) {
NewDate = 새로운 날짜 ();
newDate.getPyare () + "-" +를 반환합니다
(newDate.getMonth () + 1) + "-" +
newDate.getDate () + "" +
NewDate.gethours () + ":" +
newDate.getMinutes () + ":" +
newDate.getSeconds ();
}
NewDate를 반환하십시오.
}
</스크립트>
</head>
<body>
<script language = "javaScript"type = "text/javaScript">
//부르다
document.writeln (FormatDateTime ( "2014/04/16 22:34:45", 11);
</스크립트>
</body>
</html>