Home > Web Front-end > Front-end Q&A > javascript timestamp to datetime

javascript timestamp to datetime

WBOY
Release: 2023-05-15 19:38:06
Original
1084 people have browsed it

JavaScript is a powerful scripting language that is widely used in web front-end development and server-side programming. Converting between timestamps and datetimes is a very common operation in JavaScript.

A timestamp is the number of milliseconds since January 1, 1970 00:00:00 UTC (Coordinated Universal Time). In JavaScript, you can get the current timestamp with the following code:

var timestamp = new Date().getTime(); // 获取当前时间戳
Copy after login

However, timestamps are not intuitive and often need to be converted to datetime format for human understanding. JavaScript provides several methods for converting between timestamps and datetimes. Below we introduce these methods respectively.

Convert timestamp to date and time

To convert timestamp to date and time, you can use the toLocaleString() method of the Date object. This method will return a string based on local time in the format yyyy/MM/dd HH:mm:ss. The code is as follows:

var timestamp = 1626194523854; // 假设时间戳为 2021/07/14 12:15:23.854
var datetime = new Date(timestamp).toLocaleString('zh-CN', {hour12: false});
console.log(datetime); // 输出:2021/7/14 12:15:23
Copy after login

Among them, the toLocaleString() method accepts two parameters:

  • The first parameter specifies the region (locale) code, here we use zh-CN means China. The time format will vary depending on the region.
  • The second parameter is passed in as an object and is used to set the date and time format. {hour12: false} Indicates that the 24-hour clock is used instead of the 12-hour clock.

In this way, we successfully converted the timestamp into date and time format.

Convert date and time to timestamp

To convert date and time to timestamp, you can use the getTime() method of the Date object. This method returns the number of milliseconds since January 1, 1970 00:00:00 UTC (Coordinated Universal Time). The code is as follows:

var datetime = '2021-07-14T12:15:23.854Z'; // 假设日期时间为 2021/07/14 12:15:23.854 UTC
var timestamp = new Date(datetime).getTime();
console.log(timestamp); // 输出:1626292523854
Copy after login

Among them, the date and time need to meet the ISO 8601 standard format, that is, yyyy-MM-ddTHH:mm:ss.sssZ. In this format, Z indicates that the time zone is UTC, but other time zones can also be specified.

In this way, we successfully converted the date and time into a timestamp.

Performance considerations

Although the above code seems simple and easy to understand, it should be noted that each time a Date object is created, there will be a certain performance overhead, especially in In a loop or when called frequently. To improve performance, we can use a little trick to cache the timestamp as a variable and only convert it when needed, as follows:

var timestamp = 1626194523854; // 假设时间戳为 2021/07/14 12:15:23.854
var datetime = null; // 初始值为 null,表示尚未转换
var getDatetime = function() {
  if (datetime === null) {
    datetime = new Date(timestamp).toLocaleString('zh-CN', {hour12: false});
  }
  return datetime;
};
console.log(getDatetime()); // 输出:2021/7/14 12:15:23
Copy after login

In this way, on the first call to getDatetime() , the timestamp conversion operation will be performed and the result will be cached in the datetime variable. In subsequent calls, just return the cached result directly. Doing this avoids repeated timestamp conversion operations and improves code performance.

In short, conversion between timestamp and datetime in JavaScript is very important and very common. As long as we master the correct conversion method and pay attention to code performance, we can easily cope with various time conversion needs.

The above is the detailed content of javascript timestamp to datetime. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template