Four ways to convert JS time to milliseconds

Source: Internet
Author: User

There are four ways to convert time to milliseconds:

Date.parse ()
Date.utc
ValueOf ()
GetTime ()

1. Date.parse (): The method accepts a string argument that represents a date, and then tries to return the number of milliseconds for the date based on that date. ECMA-262 does not define which date format the method supports, so the behavior of this method varies depending on the browser implementation. If the incoming string cannot represent a date, it returns Nan. The method returns the millisecond value after three bits are all 0, accurate to the number of seconds, no milliseconds.

Date.parse("2012年9月9日")NaNDate.parse("2012 9 9")1347120000000 // 毫秒数转换为日期格式newDate(Date.parse("2012 9 9"))Sun Sep 09 2012 00:00:00 GMT+0800 (中国标准时间)

2. DATE.UTC (): The method also returns the number of milliseconds that represents the date, but the method accepts the parameter as the year, the month based on 0, the day of the month, the number of hours, minutes, seconds, and milliseconds. Only the first two parameters in these parameters are required.

Date.UTC(12)NaNDate.UTC(2013,4,11);1368230400000

3. ValueOf (): The method returns a millisecond representation of the date, which can be conveniently compared to the number of milliseconds returned by the method.

vardate = newDate();date.valueOf();1368283579633

4. GetTime (): Returns the number of milliseconds that represent the date, the same value returned by valueof (), and the exact number of milliseconds.

vardate = newDate();date.getTime();1368283691951

Four ways to convert JS time to milliseconds (RPM)

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.