JS time and time stamp conversion

Source: Internet
Author: User

One: Time-to-timestamp: JavaScript obtains timestamps in four ways, all by instantiating the time object new Date () to further obtain the current timestamp

1.var TIMESTAMP1 = date.parse (New Date ()); Results: 1477808630000 This method is not recommended and the millisecond level value is converted to 000

Console.log (TIMESTAMP1);

2.var timestamp2 = (new Date ()). ValueOf (); Results: 1477808630404 valueOf() Accurate timestamp values are obtained by returning the original value of the specified object through the function

Console.log (TIMESTAMP2);

3.var Timestamp3 = new Date (). GetTime (); Results: 1477808630404, the millisecond value of the current time is obtained directly by the prototype method, and the accurate

Console.log (Timestamp3);

4.var timetamp4 = number (new Date ());// results: 1477808630404, converts the time to a numeric value of type A, i.e. timestamp

Console.log (Timetamp4);

The printing results are as follows:

Second, time stamp turn time

var timestamp4 = new Date (1472048779952); //Convert to current time directly with new Date (timestamp) format

Console.log (Timestamp4);

Console.log (Timestamp4.tolocaledatestring (). Replace (/\//g, "-") + "" + timestamp4.totimestring (). substr (0, 8)); Re-use splicing regularization and other means to convert to YYYY-MM-DD HH:MM:SS format

The effect is as follows:

However, this conversion in some browsers will have undesirable effect, because the tolocaledatestring () method is different from the browser, such as IE for August 24, 2016 22:26:19 format Sogou for Wednesday, August 24, 22:39:42

It can be spliced by the date of each acquisition time, for example:

function getdate () {            var now = new Date (),                y = now.getfullyear (),                m = now.getmonth () + 1,                d = now.getdate ( );            Return y + "-" + (M < 10?) "0" + m:m + "-" + (D < 10?) "0" + d:d) + "" + now.totimestring (). substr (0, 8);        }

-Reprinted from Http://www.cnblogs.com/lmyt/p/6013097.html

JS time and time stamp conversion

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.