Home >Web Front-end >JS Tutorial >How to convert date to timestamp in javascript
Method: 1. Use the getTime() method, the syntax is "time object.getTime()"; 2. Use the valueOf() method, the syntax is "time object.valueOf()"; 3. Use parse() Method, syntax "parse(time object)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
js string conversion timestamp can use the built-in function Date (time string to be converted) to first convert to Date type, and then convert the Date type to timestamp type, where the time string has requirements, The format must be in the form of yyyy-MM-dd HH:mm:ss. Of course, it can also be just yyyy-MM-dd, which is 2013-08-30, as follows:
Here you will get a Date type data. To get the timestamp data, you need to operate the date object. There are three ways to convert the date into a timestamp, as follows:
1. var time1 = date.getTime( );
2.var time2 = date.valueOf();
##3.var time3 = Date. parse(date); The first and second types: will be accurate to milliseconds. The third type: can only be accurate to seconds. Milliseconds are replaced with 000. Note: Get The Unix timestamp can be obtained by dividing the arrived timestamp by 1000, and the value can be passed to the background to obtain it.Extended information
Date() returns the date and time of the current day. getDate() returns the day of the month (1 ~ 31) from the Date object. getDay() returns the day of the week (0 ~ 6) from the Date object. getMonth() returns the month (0 ~ 11) from the Date object. getFullYear() Returns the year as a four-digit number from a Date object. getYear() Please use getFullYear() method instead. getHours() returns the hours (0 ~ 23) of the Date object. getMinutes() returns the minutes (0 ~ 59) of the Date object. getSeconds() returns the seconds of the Date object (0 ~ 59). getMilliseconds() returns the milliseconds (0 ~ 999) of the Date object. getTime() returns the number of milliseconds since January 1, 1970. getTimezoneOffset() Returns the minute difference between local time and Greenwich Mean Time (GMT). getUTCDate() Returns the day of the month (1 ~ 31) from the Date object based on universal time. getUTCDay() Returns the day of the week (0 ~ 6) from the Date object based on universal time. getUTCMonth() Returns the month (0 ~ 11) from the Date object according to universal time. getUTCFulYear() Returns the four-digit year from a Date object based on universal time. getUTCHours() Returns the hour (0 ~ 23) of the Date object according to universal time. getUTCMinutes() Returns the minutes (0 ~ 59) of the Date object according to universal time. getUTCSeconds() returns the seconds (0 ~ 59) of the Date object according to universal time. getUTCMilliseconds() returns the milliseconds (0 ~ 999) of the Date object according to universal time. parse() Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string). setDate() sets the day of the month (1 ~ 31) in the Date object. setMonth() sets the month (0 ~ 11) in the Date object. setFullYear() Sets the year (four digits) in the Date object. setYear() Please use the setFullYear() method instead. setHours() Sets the hours (0 ~ 23) in the Date object. setMinutes() Sets the minutes (0 ~ 59) in the Date object. setSeconds() Sets the seconds (0 ~ 59) in the Date object. setMilliseconds() Sets the milliseconds (0 ~ 999) in the Date object. setTime() Sets the Date object in milliseconds. setUTCDate() Sets the day of the month (1 ~ 31) in the Date object according to universal time. setUTCMonth() Sets the month (0 ~ 11) in the Date object according to universal time. setUTCFulYear() Sets the year (four digits) in the Date object according to universal time. setUTCHours() Sets the hour (0 ~ 23) in the Date object according to universal time. setUTCMinutes() Sets the minutes (0 ~ 59) in the Date object according to universal time. setUTCSeconds() Sets the seconds (0 ~ 59) in the Date object according to universal time. setUTCMilliseconds() Sets the milliseconds (0 ~ 999) in the Date object according to universal time. toSource() Returns the source code of the object. toString() Converts Date object to string. toTimeString() Converts the time part of the Date object to a string. toDateString() Converts the date part of the Date object to a string. toGMTString() Please use toUTCString() method instead.toUTCString() Converts a Date object to a string according to universal time.
toLocaleString() Converts the Date object to a string according to the local time format.
toLocaleTimeString() Converts the time part of the Date object into a string according to the local time format. toLocaleDateString() Converts the date part of the Date object to a string according to the local time format.
UTC() Returns the number of milliseconds from January 1, 1997 to the specified date according to universal time. valueOf() returns the original value of the Date object.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to convert date to timestamp in javascript. For more information, please follow other related articles on the PHP Chinese website!