JS timestamp and time-date conversion between

Source: Internet
Author: User

Today in the work to get to the time to convert to timestamp, at a time do not know how to use, so have to find information, here specially to make a note.

1. Convert the date to a timestamp.

To convert a date to a timestamp, you first get to the date, where you can specify the date directly, or use the current date. To get the current date, we can use the new date () to get it. directly on the code.

//(1), convert the current date to a timestamp.     varnow =NewDate (); Console.log (Now.gettime ())//converts the current date to a timestamp, and the GetTime () method returns the number of milliseconds from January 1, 1970. //(2), converts the specified date to a timestamp.     vart = "2017-12-08 20:5:30";//months, days, hours, minutes, seconds if not two digits can be 0.    varT =NewDate (t);//converts the specified date to the standard date format. Fri Dec 20:05:30 gmt+0800 (China Standard Time)Console.log (T.gettime ())//Converts the converted standard date to a timestamp. 


2. Convert the timestamp to a date.

var t = 787986456465;  // when the argument is a number, then this parameter is the timestamp, which is treated as a millisecond, creating a time-date object that is specified in milliseconds from January 1, 1970. Console.log (new//  Wed Dec 1994 13:07:36 gmt+0800 (China Standard Time)var t2 = "2017-5-8 1 2:50:30 "; Console.log (new/  Mon may 12:50:30 gmt+0800 (China Standard Time)var t3 = "2017-10-1"; Console.log (new/  Sun Oct 00:00:00 gmt+0800 ( China standard Time) No timing seconds, the default is converted to 00:00:00

JS timestamp and time-date conversion between

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.