Home  >  Article  >  Web Front-end  >  How to convert date into timestamp in javascript

How to convert date into timestamp in javascript

藏色散人
藏色散人Original
2022-01-24 16:58:284482browse

Javascript method to convert date into timestamp: 1. Use Date function to convert time string into Date type; 2. Convert Date type into timestamp type through "date.getTime()" and other methods That’s it.

How to convert date into timestamp in javascript

The operating environment of this article: Windows 7 system, javascript version 1.8.5, DELL G3 computer

How to convert date to timestamp with javascript ?

Convert date to timestamp in js

#js string conversion timestamp can be converted first using the built-in function Date (the time string to be converted) It is a Date type, and then the Date type is converted into a timestamp type. The time string has requirements, and the format must be in the form of yyyy-MM-dd HH:mm:ss. Of course, it can also be just yyyy-MM-dd, that 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 and convert the date into time There are three ways to stamp, as follows:

1. var time1 = date.getTime();

2.var time2 = date.valueOf();

3.var time3 = Date.parse(date);

First and second: yes Accurate to milliseconds. The third type: can only be accurate to seconds. Milliseconds are replaced by 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.

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 video tutorial"

The above is the detailed content of How to convert date into 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