Getting Started with web front-end programming--js time Conversion

Source: Internet
Author: User
Tags diff

Timestamp format conversion

functionJsondateformat (jsondate) {//JSON date format converted to normal format    varJsondatestr = Jsondate.tostring ();//ToString () is used here to let the incoming value be a string type, in order to avoid an incoming data type that does not support the. Replace () method    Try {        varK = parseint (Jsondatestr.replace ("/date (", ""). Replace (")/", ""), 10); if(K < 0)            return NULL; varDate =NewDate (parseint ("Jsondatestr.replace" ("/date"). Replace (")/", ""), 10)); varmonth = Date.getmonth () + 1 < 10? "0" + (date.getmonth () + 1): Date.getmonth () + 1; varDay = Date.getdate () < 10? "0" +date.getdate (): Date.getdate (); varHours = Date.gethours () < 10? "0" +date.gethours (): Date.gethours (); varminutes = Date.getminutes () < 10? "0" +date.getminutes (): Date.getminutes (); varseconds = Date.getseconds () < 10? "0" +date.getseconds (): Date.getseconds (); varmilliseconds =date.getmilliseconds (); returnDate.getfullyear () + "-" + month + "-" + Day + "" + Hours + ":" + minutes + ":" +seconds; }    Catch(ex) {return"Time Format conversion error"; }}

Get Current time conversion

functionDatetimediff (diff) {varMyDate =NewDatetimeadd (' d ', diff); //get Current year    varYear =mydate.getfullyear (); //Get Current Month    varmonth = Mydate.getmonth () + 1; //Get current day    varDate =mydate.getdate (); varh = mydate.gethours ();//gets the current number of hours (0-23)    varm = Mydate.getminutes ();//gets the current number of minutes (0-59)    vars =mydate.getseconds (); varnow = year + '-' + P (month) + "-" + p (date) + "" + P (H) + ': ' + P (m) + ":" +p (s); returnNow ;}/** * Get current time*/functionp (s) {returnS < 10? ' 0 ' +s:s;}functionDatetimeadd (strinterval, number) {varDttmp =NewDate (); Switch(strinterval) { Case' s ':return NewDate (Date.parse (dttmp) + (1000 *Number ));  Case' N ':return NewDate (Date.parse (dttmp) + (60000 *Number ));  Case' H ':return NewDate (Date.parse (dttmp) + (3600000 *Number ));  Case' d ':return NewDate (Date.parse (dttmp) + (86400000 *Number ));  Case' W ':return NewDate (Date.parse (dttmp) + ((86400000 * 7) *Number ));  Case' Q ':return NewDate (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number * 3, Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());  Case' m ':return NewDate (Dttmp.getfullyear (), (Dttmp.getmonth ()) +Number , Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());  Case' Y ':return NewDate ((dttmp.getfullyear () +Number ), Dttmp.getmonth (), Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ()); }}

Getting Started with web front-end programming--js time Conversion

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.