Home >Web Front-end >JS Tutorial >How to convert date and time to timestamp in javascript

How to convert date and time to timestamp in javascript

青灯夜游
青灯夜游Original
2021-04-08 16:20:517011browse

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)".

How to convert date and time to timestamp in javascript

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( )

How to convert date and time to timestamp in javascript

2、date.valueOf()

How to convert date and time to timestamp in javascript

3. Date.parse(date)

How to convert date and time to timestamp in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn