JS timestamp and date format convert each other

Source: Internet
Author: User
Tags time and date

Timestamp: is a time representation, defined as the total number of seconds from GMT January 01, 1970 00:00 00 seconds to the present. Unix timestamps are used not only in UNIX systems, Unix-like systems, but also in many other operating systems.

1. Convert Timestamps to date formats

1 //a simple line of code2 varDate =NewDate (timestamp);//gets a Time object3  4 /**5 1. Here's how to get the time and date, what kind of format you need to stitch it up .6 2. More useful methods can be found here, http://www.w3school.com.cn/jsref/jsref_obj_date.asp7  */8Date.getfullyear ();//Get the full year (4-bit, 1970)9Date.getmonth ();//get the month (0-11, 0 for January, remember to add 1 when using)TenDate.getdate ();//Get Day (1-31) OneDate.gettime ();//Get time (number of milliseconds starting from 1970.1.1) ADate.gethours ();//gets the number of hours (0-23) -Date.getminutes ();//gets the number of minutes (0-59) -Date.getseconds ();//gets the number of seconds (0-59)
1 //For example, you need this format YYYY-MM-DD hh:mm:ss2 varDate =NewDate (1398250549490);3Y = date.getfullyear () + '-';4M = (Date.getmonth () +1 < 10? ' 0 ' + (date.getmonth () +1): Date.getmonth () +1) + '-';5D = date.getdate () + ";6h = date.gethours () + ': ';7m = date.getminutes () + ': ';8s =date.getseconds ();9Console.log (Y+m+d+h+m+s);//Yes, hemp disc .Ten //output Result: 2014-04-23 18:55:49

2. Convert date format to timestamp

1 //It 's simple, too .2 varStrtime = ' 2014-04-23 18:55:49:123 ';3 varDate =NewDate (strtime);4 //Pass in a time format, if not passed is to get the current time, this does not compatible with Firefox. 5 //can do this6 varDate =NewDate (Strtime.replace (/-/g, '/')));7  8 //There are three ways to get, and in the back there will be three different ways9Time1 =date.gettime ();TenTime2 =date.valueof (); OneTime3 =Date.parse (Date); A   - /*  - three ways to get the difference: the First, second: accurate to milliseconds - The third type: Only accurate to the second, milliseconds will be replaced by the - For example, the result of the above code output (the difference can be seen at one glance): - 1398250549123 + 1398250549123 - 1398250549000 + */

3. The Date () parameter is in the form of 7

1 New Date ("Month dd,yyyy hh:mm:ss"); 2 New Date ("Month dd,yyyy"); 3 New Date ("Yyyy/mm/dd hh:mm:ss"); 4 New Date ("Yyyy/mm/dd"); 5 New Date (YYYY,MTH,DD,HH,MM,SS); 6 New Date (YYYY,MTH,DD); 7 New Date (MS);

Like what:

 1  new  Date ("September 16,2016 14:15:05 " 2  new  Date ("September 16,2016"  3  new  Date ("2016/09/16 14:15:05"  4  new  Date ("2016/09/16" );  5  new  Date (2016,8,16,14,15,5); //  month from 0~11  6  new  Date (2016,8,16  7  new  Date (1474006780); 

JS timestamp and date format convert each other

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.