JS implements date conversion/interchange functions like strtotime functions and Timetostr in PHP

Source: Internet
Author: User

<script type= "Text/javascript" >  //date (format: yyyy-mm-dd h:i:s)  ---converted to UNIX timeline in seconds ( Format: XXXXXX)   Method One:   //the sky star: Http://blog.csdn.net/zqtsx  function strtotime1 ( datetime) {       var tmp_datetime = datetime.replace (/:/g, '- ');        tmp_datetime = tmp_datetime.replace (/ /g, '-');        var arr = tmp_datetime.split ("-");        var y=arr[0];      var m=arr[1]-1;       var d=arr[2];      var h=arr[3]-8; /// Compatible with eight-hour jet lag problem       var i=arr[4];      var s= arr[5];      //compatible without "time: minute: Seconds" mode       if (arr[3]== ' Undefined '  | |  isnan (h)) {        h=00;      }       if (arr[4]== ' undefined '  | |  isnan (i)) {        i=00;      }       if (arr[5]== ' undefined '  | |  isnan (s)) {        s=00;      }       var now = new date (DATE.UTC (y,m,d,h,i,s));        return parseint (Now.gettime ()/1000);    }  //Date ( Format: yyyy-mm-dd h:i:s)   Convert---to UNIX timeline in seconds (format: xxxxxx)   Method two:   function strtotime ( datetime) {      var _ = datetime.split ('   ');       var ymd = _[0];      var his =  _[1];      var str = ymd.split ('-');      var  Fix = his.split (': ');       var year  = str[0]  - 0;       var month = str[1] - 0  - 1;       var day   = str[2] - 0;        var hour   = fix[0] - 0;        var minute = fix[1] - 0;        var second = fix[2] - 0;      //compatible without "time : minutes: Seconds "mode       if (fix[0]== ' undefined '  | |  isnan (hour)) {        hour=00;       }      if (fix[1]== ' undefined '  | |  isnan (minute)) {        minute=00;       }      if (fix[2]== ' undefined '  | |  isnan (second)) {        second=00;       }      time =  (New date (year, month, day,  Hour, minute, second). GetTime ();       //alert (time);       return parseint ( time / 1000 );   }  // Date 24-hour format, single-digit pre-complement 0   //extract Sky Star: Http://blog.csdn.net/zqtsx  function fillZero (v) {       if (v<10) {v= ' 0 ' +v;}       return v;  }  //the Unix timeline in seconds converted to date   The converted format is:yyyy-mm-dd h:i:s    method one:   function timetostr1 (Unix)  {    &nbSp;   var now = new date (parseint (Unix)  * 1000);   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//12 hour Format Date conversion Get method The method cannot be applied to a reverse conversion because it is imprecise       //return  now.tolocalestring (). Replace (/year | month/g,  '-'). Replace (/day/g,  "). Replace (/am | pm/g, ');        //accurate to 24-hour format       return now.getfullyear () + '-' +fillzero (Now.getmonth () +1) + '-' +fillzero (Now.getdate ()) + '   ' +fillzero (now.gethours ()) + ': ' +fillzero ( Now.getminutes ()) + ': ' +fillzero (Now.getseconds ());   }  //linux time seconds   Convert to date format   / /uninxtime Unix time   //type date format type=12 in seconds is in 12-hour format, other or not filled in 24-hour format   //show_type show days of the week Show_ Type=1 that is to display the day of the week, and with the date of the year of the Chinese characters, do not fill in or other default is not realistic weeks   //method two:   //Harvest Sky Star: HTTP://BLOG.CSDN.NET/ZQTSX   function timetostr (Unixtime,type,show_type) {         Var d=new date (ParseiNT (unixtime)  * 1000);         var week=[' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '];        y=d.getfullyear ();         m=fillzero (D.getmonth () +1);         d=fillzero (D.getdate ());         w=week[d.getday ()]; //get the day of the week         h=fillzero (D.gethours ());         i=fillzero (D.getminutes ());         s=fillzero (d.getSeconds ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//12 Hour display mode          if (type && type==12) {          // To display more time types such as early noon can be added below the judgment           if (h<=12) {             h= ' Morning   ' +h;          }else  if (h>12 && h<24) {             h-=12;            h= ' pm   ' + Fillzero (H);           }else if (H==24) {             h= ' Wee  00 ';           }        }         if (show_type && show_type== ' 1 ')  return y+ ' year ' +M+ ' month ' +D+ ' Day ' + '   ' +w+ '   ' +h+ ': ' +i+ ': ' +s;        return y+ '-' +m+ '-' +D+ '   ' + H + ': ' +i+ ': ' +s;        //can add expansion per second update time   build   pick Sky Star:/http blog.csdn.net/zqtsx  }  </script> 


This article is from the "Harvest Sky Star" blog, please be sure to keep this source http://zqtsx.blog.51cto.com/3452697/1429357

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.