1. What is a timestamp?
Timestamp (Timestamp), also known as "UNIX timestamp", is a digital format used to mark dates and times. It represents since January 1970 The number of seconds since 00:00:00 on the 1st. Timestamps are often used to exchange time information between different computer systems, and are also widely used for program timing and computer alarm clocks.
In Node.js, we can use the built-in Date object to handle timestamps. The Date object provides us with a series of methods to obtain and process dates and times. Below we will introduce some commonly used methods.
2. Get the current timestamp
We can use the getTime() method of the Date object to get the timestamp of the current time. The getTime() method returns 00 since January 1, 1970: For the number of milliseconds since 00:00, we can divide it by 1000 to get the number of seconds. The code is as follows:
let timestamp = new Date().getTime() / 1000; console.log(timestamp);
The output result should be a ten-digit number, for example: 1623891191.
3. Convert timestamp to date
Sometimes we need to convert timestamp to date. The Date object provides the fromEpochTime() method to achieve this function. The fromEpochTime() method accepts a timestamp as a parameter and returns a new Date object. The code is as follows:
let timestamp = 1623891191; let date = new Date(0); // 参数0代表1970年1月1日00:00:00 date.setUTCSeconds(timestamp); console.log(date.toLocaleDateString()); // 输出日期,例如:2021/6/17 console.log(date.toLocaleTimeString()); // 输出时间,例如:8:53:11 AM console.log(date.toLocaleString()); // 输出日期和时间,例如:2021/6/17 8:53:11 AM
In the above code, we first create a Date object and initialize it to January 1, 1970. 00:00:00. We then set the timestamp to this Date object using the setUTCSeconds() method, and get the date and time strings using the toLocaleDateString(), toLocaleTimeString(), and toLocaleString() methods.
4. Convert date to timestamp
Sometimes we need to convert date to timestamp, we can use the getTime() method of Date object. The getTime() method returns the number of milliseconds since 00:00:00 on January 1, 1970. We can divide it by 1000 to get the number of seconds. The code is as follows:
let date = new Date('2021/6/17 8:53:11 AM'); let timestamp = date.getTime() / 1000; console.log(timestamp);
The output result should be a ten-digit number Number, for example: 1623891191.
5. Calculate timestamps and dates
Sometimes we need to calculate timestamps, for example: calculate the time difference between two timestamps, or calculate the time between a certain timestamp Basically add a certain amount of time. We can use the set and get series methods of the Date object to achieve this function. The following is an example:
let timestamp1 = 1623891191; let date = new Date(0); // 参数0代表1970年1月1日00:00:00 date.setUTCSeconds(timestamp1); console.log(date.toLocaleString()); // 输出:2021/6/17 8:53:11 AM let timestamp2 = timestamp1 + 60 * 60 * 24 * 7; // 在timestamp1的基础上加上7天 date = new Date(0); date.setUTCSeconds(timestamp2); console.log(date.toLocaleString()); // 输出:2021/6/24 8:53:11 AM let diff = timestamp2 - timestamp1; // 计算两个时间戳之间的时间差,单位为秒 console.log(diff); // 输出:604800
In the above code, we first convert a timestamp into a Date object and use the toLocaleString() method Output string representation. We then calculated another timestamp by adding 60 60 24 * 7 (the number of seconds in 7 days), then converted it to a Date object and output the string representation again. Finally, we get the time difference between the two timestamps by simple subtraction.
6. Summary
The Date object of Node.js provides us with rich time processing functions, which can easily handle conversion and calculation between timestamps, dates and times. In actual development, we often need to use these methods to handle time-related operations, so it is very important to learn to use Date objects.
The above is the detailed content of How nodejs handles timestamps. For more information, please follow other related articles on the PHP Chinese website!