SoFunction
Updated on 2025-03-01

Summary of the method of obtaining timestamps in JavaScript

1. JavasCRIPT time to time stamp

There are five ways to obtain timestamps in JavaScript. The last four methods are to further obtain the current timestamp by instantiating the time object new Date(). JavaScript mainly uses the time object Date to process time.

Method 1: ()

() You can get the current timestamp:

(()) //1642471441587

Method 2: ()

() Convert strings or time objects directly into timestamps:

(new Date()) //("2022/1/18 10:05") //1642471500000

Note: This method is not recommended, the value at the millisecond level is converted to 000.

Method 3: valueOf()

Obtain the exact timestamp value by returning the original value of the specified object by the valueOf() function:

(new Date()).valueOf() //1642471624512

Method 4: getTime()

Directly obtain the millisecond value of the current time through the prototype method, accurate:

new Date().getTime() //1642471711588

Method 5: Number

Convert a time object into a number type value, i.e. a time stamp

Number(new Date()) //1642471746435

2. JS timestamp to time

We can use the new Date (timestamp) format to convert to obtain the current time, such as:

new Date(1472048779952)Wed Aug 24 2016 22:26:19 GMT+0800 (China Standard Time)

Note: The timestamp parameter must be of Number type. If it is a string, the parsing result is: Invalid Date.

If the backend directly returns the timestamp to the frontend, how can the frontend convert it? The following are two implementation methods

Method 1: Generate '2022/1/18 10:09 AM' format

function getLocalTime(n) {return new Date(parseInt(n)).toLocaleString().replace(/:\d{1,2}$/,' ');}getLocalTime(1642471746435) //'2022/1/18 10:09 am'

You can also use it as follows: If you want to take a few digits, please note that spaces are also counted!

function getLocalTime(n) {return new Date(parseInt(n)).toLocaleString().substr(0,14)}getLocalTime(1642471746435) //'2022/1/18 10 am'

Or use the regular:

function  getLocalTime(n){return new Date(parseInt(n)).toLocaleString().replace(/Year|moon/g, "-").replace(/day/g, " ");}getLocalTime  (1642471746435)  //'2022/1/18 10:09:06 AM'

Method 2: Generate 'yyyy-MM-dd hh:mm:ss' format

First convert it to a data object, and then use splicing rules and other means to achieve it:

function getData(n){n=new Date(n)return ().replace(///g, "-") + " " + ().substr(0, 8)}getData(1642471746435) //'2022-1-18 10:09:06'

However, this conversion will have an unsatisfactory effect on some browsers, because the toLocaleDateString() method varies from browser to browser. For example, IE is in the format "August 24, 2016 22:26:19"; Sogou is in the format "Wednesday, August 24, 2016 22:39:42"

You can splice the year, month and day of the time separately, so that the compatibility is better:

function getData(n) {let now = new Date(n),y = (),m = () + 1,d = ();return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + ().substr(0, 8);}getData(1642471746435) //'2022-1-18 10:09:06'

3. Popularization of knowledge

1. Current system locale format (toLocaleDateString and toLocaleTimeString)

(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString()//'2022/1/18 10:30:30 AM'

2. Ordinary strings (toDateString and toTimeString)

(new Date()).toDateString() + " " + (new Date()).toTimeString()//'Tue Jan 18 2022 10:30:50 GMT+0800 (China Standard Time)'

3. Greenwich Standard Time (toGMTString)

(new Date()).toGMTString()//'Tue, 18 Jan 2022 02:31:10 GMT'

4. Global Standard Time (toUTCString)

(new Date()).toUTCString()//'Tue, 18 Jan 2022 02:31:25 GMT'

5. Date object string (toString)

(new Date()).toString()'Tue Jan 18 2022 10:31:44 GMT+0800 (China Standard Time)'

Date object constructor

Date objects have multiple constructors:

new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)

Description of the parameter of the Date object constructor:

milliseconds - The number of milliseconds from the start time defined internally by JavaScript on January 1, 1970
datestring – The date and time represented by the string. This string can be converted using ()
year -  A four-digit year, if the value is 0-99, add 1900 to it
Month - 0 (represents January)-11 (represents December)
day-  Date between 1-31
hours - 0 (representing midnight)-23 hours
minutes -                                                             �
seconds -  The number of seconds between 0-59
microseconds - Milliseconds between 0-999

Date object return value

If there are no parameters, the current date will be returned;

If the parameter is a number, treat the number as a millisecond value and convert it to a date

If the parameter is a string, treat the string as a string representation of the date and convert it to a date

You can also use six constructors to accurately define and return time

var d1 = new Date();(());var d2 = new Date("2009-08-08 12:12:12); (()); var d3 = new Date(2009, 8, 8); (());

As a built-in object of JavaScript, Date must be created in new way.

The representation of the Date object in JavaScript is the number of milliseconds (timestamps) from midnight on January 1, 1970 (GMT time). We also call the internal representation of Date here the timestamp.

You can use getTime() to convert the Date object to the timestamp of Date, and the method setTime() can convert the timestamp of Date into the standard form of Date.

Date function use syntax

date.Method name (parameter 1, parameter 2,...);
Date.Method name();
date represents an instance of a date object, Date represents a date object, date. The method name calls the member function of the object
Date. The static function called as an object with the method name

var d=new Date();var d2=();

This is the end of this article about the summary of JavaScript's method of obtaining timestamps. For more related JavaScript timestamp content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!