Time processing in javascript: Convert timestamp or Time object to string format

Source: Internet
Author: User
Tags local time string format

Problem background: Want to turn a time directly into a string format

A tolocalestring () is found by checking the API to convert the Date object to a string based on the local time format

New Date (). tolocalestring (); // "2018/5/31 pm 1:43:06"

But the default is 12-hour system, will take this morning afternoon, so certainly do not need, continue to check, found can be configured

var New 3600000 ;( New Date (ss). tolocalestring (' Chinese ', {hour12:false})). Replace (/\//G, '-'); // "2018-5-31 14:40:32"

This turns into the format I need.

JS's Date object method, you can see: JavaScript Date Object

1,JS get the current timestamp method

var timestamp1 = Date.parse (new  Date ()); var timestamp2 = (new  Date ()). ValueOf (); var New Date (). GetTime (); Console.log (Timestamp1,timestamp2,timestamp3) // vm140:4 1527745698000 1527745698616 1527745698616

The first: The time stamp obtained is precision to the second level, the millisecond is changed to 000 display;

The second and third is the precision to the millisecond level, which gets the timestamp of the current millisecond.

var New Date (); var newDay2 = Date.parse (new  Date ()); var newDay3 = +new  Date (); Console.log (number (newDay1), newday2,newday3)// vm160:4 1527745994726 1527745994000 1527745994726

The first and third, the timestamp returned by the method using the numeric object, is exactly the millisecond,

The second Date object's Date.parse () method is only accurate to the second, the latter three bits are filled with 0, so the first one is recommended

2. Convert Timestamps to date objects

New Date (1527745994726)//Thu 2018 13:53:14 gmt+0800 (China Standard Time)

Time processing in javascript: Convert timestamp or Time object to string format

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.