The conversion between JS timestamp and date format

Source: Internet
Author: User

Here is a summary of the time stamp in JS and the date format of the mutual conversion:

1. Convert the timestamp to a date format:

functionTimestamptotime (timestamp) {varDate =NewDate (timestamp * 1000);//The timestamp is 10-bit *1000 and the timestamp is 13-bit, so you don't need to multiplyY = date.getfullyear () + '-'; M= (Date.getmonth () +1 < 10? ' 0 ' + (date.getmonth () +1): Date.getmonth () +1) + '-'; D= Date.getdate () + "; H= date.gethours () + ': '; M= date.getminutes () + ': '; S=date.getseconds (); returny+m+d+h+m+s; } timestamptotime (1403058804); Console.log (Timestamptotime (1403058804));//2014-06-18 10:33:24

Note: If it is a Unix timestamp, remember to multiply by 1000. For example: Php function time () Gets the timestamp multiplied by 1000.

2. Convert the date format to a timestamp:

var New Date (' 2014-04-23 18:55:49:123 ');     // There are three ways of getting    var time1 = date.gettime ();     var time2 = date.valueof ();     var time3 = date.parse (Date);    Console.log (time1); // 1398250549123    Console.log (time2); // 1398250549123    Console.log (Time3); // 1398250549000

The difference between the above three methods of acquisition:

First, second: accurate to milliseconds

The third type: only accurate to seconds, milliseconds with 000 instead

The difference between the above three output results can be observed

Note: The time stamp obtained is divided by 1000 to obtain a UNIX timestamp, which can be passed to the background.

The conversion between JS timestamp and date 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.