Home > Web Front-end > JS Tutorial > javascript format date and time function_time and date

javascript format date and time function_time and date

WBOY
Release: 2016-05-16 19:03:45
Original
1066 people have browsed it
Copy code The code is as follows:

function DateUtil(){}/***Function: Format time *Example: DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 UTC 0800 2006 "); *Return: 2006/11/09*/DateUtil.Format=function(fmtCode,date){ var result,d,arr_d; var patrn_now_1=/^y{4}-M{ 2}-d{2}sh{2}:m{2}:s{2}$/; var patrn_now_11=/^y{4}-M{1,2}-d{1,2}sh{1 ,2}:m{1,2}:s{1,2}$/; var patrn_now_2=/^y{4}/M{2}/d{2}sh{2}:m{2}:s {2}$/; var patrn_now_22=/^y{4}/M{1,2}/d{1,2}sh{1,2}:m{1,2}:s{1,2}$ /; var patrn_now_3=/^y{4} year M{2} month d{2} day sh{2} hour m{2} minute s{2} second$/; var patrn_now_33=/^y{4} year M{1,2} month d{1,2} day sh{1,2} hour m{1,2} minute s{1,2} second $/;        var patrn_date_1=/^y{4}-M{ 2}-d{2}$/; var patrn_date_11=/^y{4}-M{1,2}-d{1,2}$/; var patrn_date_2=/^y{4}/M{2} /d{2}$/; var patrn_date_22=/^y{4}/M{1,2}/d{1,2}$/; var patrn_date_3=/^y{4} year M{2} month d {2}day$/; var patrn_date_33=/^y{4} year M{1,2} month d{1,2} day$/; var patrn_time_1=/^h{2}:m{2}:s {2}$/; var patrn_time_11=/^h{1,2}:m{1,2}:s{1,2}$/; var patrn_time_2=/^h{2} hours m{2} minutes s {2} seconds $/; var patrn_time_22=/^h{1,2} hours m{1,2} minutes s{1,2} seconds$/; var patrn_time_22=/^h{1,2} minutes s{1,2} seconds$/; hh:mm:ss";} if(date){ d=new Date(date); if(isNaN(d)){ msgBox("Illegal time parameter nCorrect time example:nThu Nov 9 20:30:37 UTC 0800 2006n or 2006/ 10/17"); result=arr_d.yyyy "-" arr_d .MM "-" arr_d.dd " " arr_d.hh ":" arr_d.mm ":" arr_d.ss; } else if(patrn_now_11.test(fmtCode)) { arr_d=splitDate(d); result=arr_d.yyyy "-" arr_d.MM "-" arr_d.dd " " arr_d.hh ":" arr_d.mm ":" arr_d.ss; } else if(patrn_now_2.test(fmtCode)) { arr_d=splitDate(d,true) ;       result=arr_d.yyyy "/" arr_d.MM "/" arr_d.dd " " arr_d.hh ":" arr_d.mm ":" arr_d.ss; splitDate(d);      result=arr_d.yyyy "/" arr_d.MM "/" arr_d.dd " " arr_d.hh ":" arr_d.mm ":" arr_d.ss; } else if(patrn_now_3.test(fmtCode) )             arr_d=splitDate(d,true); "; } else if(patrn_now_33.test(fmtCode)) { arr_d=splitDate(d); result=arr_d.yyyy "year" arr_d.MM "month" arr_d.dd "day" " " arr_d.hh "hour" arr_d .mm "minutes" arr_d.ss "seconds"; } else if(patrn_date_1.test(fmtCode)) { arr_d=splitDate(d,true); result=arr_d.yyyy "-" arr_d.MM "-" arr_d.dd ; } else if(patrn_date_11.test(fmtCode)) { arr_d=splitDate(d); result=arr_d.yyyy "-" arr_d.MM "-" arr_d.dd; }    else if(patrn_date_2.test(fmtCode))    {        arr_d=splitDate(d,true);        result=arr_d.yyyy "/" arr_d.MM "/" arr_d.dd;    }    else if(patrn_date_22.test(fmtCode))    {        arr_d=splitDate(d);        result=arr_d.yyyy "/" arr_d.MM "/" arr_d.dd;    }    else if(patrn_date_3.test(fmtCode))    {        arr_d=splitDate(d,true);        result=arr_d.yyyy "年" arr_d.MM "月" arr_d.dd "日";    }    else if(patrn_date_33.test(fmtCode))    {        arr_d=splitDate(d);        result=arr_d.yyyy "年" arr_d.MM "月" arr_d.dd "日";    }    else if(patrn_time_1.test(fmtCode)){        arr_d=splitDate(d,true);        result=arr_d.hh ":" arr_d.mm ":" arr_d.ss;    }    else if(patrn_time_11.test(fmtCode)){        arr_d=splitDate(d);        result=arr_d.hh ":" arr_d.mm ":" arr_d.ss;    }    else if(patrn_time_2.test(fmtCode)){        arr_d=splitDate(d,true);        result=arr_d.hh "时" arr_d.mm "分" arr_d.ss "秒";    }    else if(patrn_time_22.test(fmtCode)){        arr_d=splitDate(d);        result=arr_d.hh "时" arr_d.mm "分" arr_d.ss "秒";    }    else{        msgBox("没有匹配的时间格式!");        return;    }       return result;};function splitDate(d,isZero){    var yyyy,MM,dd,hh,mm,ss;    if(isZero){         yyyy=d.getYear();         MM=(d.getMonth() 1)<10?"0" (d.getMonth() 1):d.getMonth() 1;         dd=d.getDate()<10?"0" d.getDate():d.getDate();         hh=d.getHours()<10?"0" d.getHours():d.getHours();         mm=d.getMinutes()<10?"0" d.getMinutes():d.getMinutes();         ss=d.getSeconds()<10?"0" d.getSeconds():d.getSeconds();    }else{         yyyy=d.getYear();         MM=d.getMonth() 1;         dd=d.getDate();         hh=d.getHours();         mm=d.getMinutes();         ss=d.getSeconds();      }    return {"yyyy":yyyy,"MM":MM,"dd":dd,"hh":hh,"mm":mm,"ss":ss};  }function msgBox(msg){    window.alert(msg);}

例子:

[Ctrl A 全选 注:如需引入外部Js需刷新才能执行
]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template