JS implementation timestamp and time-date conversion

Source: Internet
Author: User

JS implementation of timestamps and time-date conversions:
The conversion of timestamps and time dates is a common operation, and the following is an example of how you can convert between them by code examples.
It is recommended that you refer to the introduction to the date () constructor parameter in JavaScript in advance.
I. Time-date conversion to timestamp:
Now there's a time date: "2013/5/12 20:10:20", which is converted to a timestamp form:
The code is as follows:

var datestr= "2013/5/12 20:10:20"; var date=New  date (DATESTR); Console.log (Date.gettime ());

Use the gettime () function to get the timestamp of a specified time-date object, as described in the GetTime () method section of the JavaScript Date object .
In fact, there are many other methods, here is not introduced, just need to know this way can be, others can accumulate slowly.
Two. Time stamp conversion to event date:
Example code one:

var date=New date (1368360620000); Console.log (date.tolocalestring ());

For more information, refer to the tolocalestring () method section of the JavaScript Date object .
Example two:

functionFormatDate (now) {varYear=now.getyear (); varMonth=now.getmonth (+1); varDate=now.getdate (); varHour=now.gethours (); varMinute=now.getminutes (); varSecond=now.getseconds (); returnyear+ "-" +month+ "-" +date+ "" +hour+ ":" +minute+ ":" +second; }   varD=NewDate (1368360620000); Console.log (FormatDate (d));

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=11849

For more information, refer to: http://www.softwhy.com/javascript/

JS implementation timestamp and time-date 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.