The mutual transfer between JS timestamp and date format

Source: Internet
Author: User

1. Convert Timestamps to date formats
//a simple line of codevarDate =NewDate (timestamp);//get a Time object note: If the timestamp is Uinx, remember to multiply at 1000. For example, PHP function time () to get the timestamp will be multiplied/** 1. Here's how to get the time and date, and what kind of format you need to stitch it up for 2. More useful ways to find it here. http://www.w3school.com.cn/jsref/jsref_obj_date.asp */date.getfullyear (); //Get the full year (4-bit, 1970)Date.getmonth ();//get the month (0-11, 0 for January, remember to add 1 when using)Date.getdate ();//Get Day (1-31)Date.gettime ();//Get time (number of milliseconds starting from 1970.1.1)Date.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)
Example
// For example, you need this format YYYY-MM-DD hh:mm:ss var New Date (1398250549490= date.getfullyear () + '-'= (Date.getmonth () +1 < 10?) ' 0 ' + (date.getmonth () +1): Date.getmonth () +1) + '-'= date.getdate () + '= date.gethours () + ': '= date.getminutes () + ': '= date.getseconds (); Console.log (Y// Yes Hemp disc //  Output Result: 2014-04-23 18:55:49
2. Convert date format to timestamp
//It 's simple, too .varStrtime = ' 2014-04-23 18:55:49:123 ';varDate =NewDate (Strtime);//Pass in a time format, if not passed is to get the current time, this does not compatible with Firefox. //can do thisvararr = strtime.replace (/|:/ G, '-'). Split ('-');d ate =NewDate (DATE.UTC (arr[1], arr[2], arr[3], arr[4], arr[5]));//There are three ways to get, and in the back there will be three different waysTime1 =date.gettime (); Time2=date.valueof (); Time3=Date.parse (Date);/*three ways to get the difference: first, second: it will be accurate to the millisecond third: only accurate to the second, milliseconds will be replaced with the result of the above code such as the above output (can see the difference at one glance): 139825054912313982505491231398250549000*/

The mutual transfer between JS timestamp and date format

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.