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

青灯夜游
Release: 2023-01-04 09:35:17
Original
7047 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template