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日" ) NaN Date.parse( "2012 9 9" ) 1347120000000 // 毫秒数转换为日期格式 new Date(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) NaN Date.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.
var date = new Date(); 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.
var date = new Date(); date.getTime(); 1368283691951 |
Four ways to convert JS time to milliseconds (RPM)