JavaScript Format Date Time function _ time Date

Source: Internet
Author: User
Tags getdate
Copy Code code as follows:

Function dateutil () {}/*** feature: 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) {    &NBSP;VAR&NBSP;RESULT,D,ARR_D;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;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}$/;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;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}$/;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;PATRN_NOW_3=/^Y{4} m{2} month d{2} Day \sh{2} m{2} s{2} seconds $/;   &NBSP;&NBSP;VAR&NBSP;PATRN_NOW_33=/^Y{4} m{1,2} month d{1,2} Day \sh{1,2} m{1,2} s{1,2} seconds $/;         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} m{1,2} month d{1,2} day $/;         var patrn_time_1=/^h{2}:m{2}:s{2}$/;    var &NBSP;PATRN_TIME_11=/^H{1,2}:M{1,2}:S{1,2}$/;&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;PATRN_TIME_2=/^H{2} m{2} s{2} seconds $/ ;     var patrn_time_22=/^h{1,2} m{1,2} s{1,2} seconds $/;         if (!fmtcode) {fmtcode= "Yyyy/mm/dd hh:mm:ss";}     if (date) {        d=new date (date);         if (isNaN (d)) {             msgbox ("The time parameter is illegal \ n the correct time example: \nthu nov 9  20:30:37 utc+0800 2006\n or \n2006/       10/17 ");             return;}     }else{        d=new date ();     }    if (Patrn_now_1.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;&NBSP;&NBSP;&NBSP;&NBSP;} "     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)) &NBSP;&NBSP;&NBSp; {        arr_d=splitdate (d,true);         result=arr_d.yyyy+ "/" +arr_d.mm+ "/" +arr_d.dd+ " " +arr_d.hh+ ":" +arr_d.mm+ ":" +arr_ D.ss;    }    else if (Patrn_now_22.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_3.test (fmtCode))      {        arr_d=splitdate (d,true);         result=arr_d.yyyy+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day" + " " +arr_d.hh+ "when" +arr_d.mm+ "points" +arr_d.ss+ "Seconds";     }    else if (Patrn_now_33.test (fmtCode))      {   &nbsP;    arr_d=splitdate (d);         result=arr_d.yyyy + "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day" + " " +arr_d.hh+ "when" +arr_d.mm+ "" +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+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day";    }     else if (Patrn_date_33.test (fmtcode))     {         arr_d=splitdate (d);         result=arr_d.yyyy+ "Year" +arr_d.MM+ "Month" +arr_d.dd+ "Day";     }    else if (Patrn_time_1.test (FmtCode)) {      &nbSp;  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+ "seconds";     }    else if (Patrn_time_22.test (fmtcode)) {         arr_d=splitdate (d);         result=arr_d.hh+ "when" +arr_d.mm+ "points "+arr_d.ss+" Seconds ";    }    else{         msgbox ("no match.Time format! ");         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":d D, "hh": hh, "mm": MM, "ss": Ss};  }function msgbox (msg) {     Window.alert (msg);

Example:
<p>js format Time _ Yun-Habitat community </p> <input type= "value=" Thu Nov 9 20:30:37 utc+0800 2008 "id=" Cssrain "size=" "/>" <input type= "" id= "To_format"/> <input type= "button" value= "Test" onclick= "format_to ()"/> <script LA Nguage= "JavaScript" > Function Dateutil () {}/*** function: Formatting 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} m{2} month d{2} Day \sh{2} m{2 s{2} seconds $/; var patrn_now_33=/^y{4} m{1,2} month d{1,2} Day \sh{1,2} m{1,2 s{1,2} seconds $/; 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} m{2} month d{2} day $/; var patrn_date_33=/^y{4} 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} m{2} s{2} seconds $/; var patrn_time_22=/^h{1,2} m{1,2} s{1,2} seconds $/; if (!fmtcode) {fmtcode= "Yyyy/mm/dd hh:mm:ss";} if (date) {d=new date (date); if (isNaN (d)) {MsgBox ("Time parameter illegal \ n correct time sample: \nthu Nov 9 20:30:37 utc+0800 2006\n or \n2006/10/17"); return;} }else{d=new Date (); } if (Patrn_now_1.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; " 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; else if (Patrn_now_22.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_3.test (Fmtcode)) {arr_d=splitdate (d,true); result=arr_d.yyyy+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day" + "" +arr_d.hh+ "when" +arr_d.mm+ "minute" +arr_d.ss+ "seconds"; 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+ "when" +arr_d.mm+ "minute" +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+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day"; else if (Patrn_date_33.test (Fmtcode)) {arr_d=splitdate (d); result=arr_d.yyyy+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day"; 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+ "seconds"; else if (Patrn_time_22.test (Fmtcode)) {arr_d=splitdate (d); result=arr_d.hh+ "+arr_d.mm+" +arr_d.ss+ "seconds"; } else{MsgBox ("No matching time format!"); 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":d D, "hh": hh, "mm": MM, "ss": ss}; }function MsgBox (msg) {Window.alert (msg);} function format_to () {var c = document.getElementById ("Cssrain"). Value; var a = Dateutil.format ("Yyyy/mm/dd", c); document.getElementById ("To_format"). Value= A; } </SCRIPT>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.