Home >Web Front-end >JS Tutorial >How to convert date and time to timestamp in javascript
Conversion method: 1. Use the getTime() method, the syntax is "date object.getTime()"; 2. Use the valueOf() method, the syntax is "date object.valueOf()"; 3. Use parse( ) method, syntax "Date.parse(date 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 obtain the timestamp data, you need to operate the date object. There are three ways to convert the date into a timestamp, as follows:
1, date.getTime( )
2、date.valueOf()
3. Date.parse(date)
Description: The first and second types: will be accurate to milliseconds, the third type: can only be accurate to seconds, Replace milliseconds with 000. Note: The obtained timestamp is divided by 1000 to obtain the Unix timestamp, which can be obtained by passing the value to the background.
Recommended learning: javascript advanced tutorial
The above is the detailed content of How to convert date and time to timestamp in javascript. For more information, please follow other related articles on the PHP Chinese website!