Home > Web Front-end > Front-end Q&A > javascript timestamp conversion cycle

javascript timestamp conversion cycle

王林
Release: 2023-05-09 10:25:06
Original
662 people have browsed it

With the continuous development of the Internet and the popularity of applications, timestamps, as a commonly used time representation method, are widely used in various data interaction scenarios. For front-end developers, it is often necessary to convert timestamps so that they can be presented to users more intuitively, while also facilitating back-end data interaction.

In JavaScript, timestamp is a unit of time expressed in milliseconds, but in actual use we are more accustomed to expressing time in units of cycles, such as year, month, day, hour, minute, and second. wait. Therefore, this article will describe how to use JavaScript to convert a timestamp into a representation of time in various periodic units.

  1. Convert timestamp to time string

In JavaScript, we can use the toLocaleString() method of the Date object to convert the timestamp into a local time string. This method returns a string. The format of the string varies according to different locales, such as "2022/01/01 1:00:00 PM".

Code example:

const timestamp = 1641010800000; // 时间戳,单位为毫秒
const date = new Date(timestamp);
const localTimeString = date.toLocaleString(); // 本地时间字符串
console.log(localTimeString);
Copy after login
  1. Convert timestamp to date string

In addition to converting timestamp to local time string, you can also It is converted into a string in calendar date format, that is, only the year, month, and day parts are retained. This can be achieved by using the toLocaleDateString() method of the Date object.

Code example:

const timestamp = 1641010800000; // 时间戳,单位为毫秒
const date = new Date(timestamp);
const dateString = date.toLocaleDateString(); // 日期字符串
console.log(dateString);
Copy after login
  1. Convert timestamp to time string (excluding date)

If we only need to keep the part of the time, we can Use the Date object's toLocaleTimeString() method.

Code example:

const timestamp = 1641010800000; // 时间戳,单位为毫秒
const date = new Date(timestamp);
const timeString = date.toLocaleTimeString(); // 时间字符串(不包含日期)
console.log(timeString);
Copy after login
  1. Convert timestamp to time object

The Date object in JavaScript can not only convert timestamp to string format, It can also be converted into a time object, containing information such as year, month, day, hour, minute, second, etc.

Code example:

const timestamp = 1641010800000; // 时间戳,单位为毫秒
const date = new Date(timestamp);
const year = date.getFullYear(); // 年
const month = date.getMonth() + 1; // 月(注意要加上1)
const day = date.getDate(); // 日
const hour = date.getHours(); // 时
const minute = date.getMinutes(); // 分
const second = date.getSeconds(); // 秒
console.log(year, month, day, hour, minute, second);
Copy after login
  1. Convert timestamp to hour, minute and second representation

Sometimes we only need to get the hour, minute and second part of the time, which can be calculated The difference between the timestamp and the zero time timestamp of the day, and perform unit conversion to obtain the representation of hours, minutes and seconds.

Code sample:

const timestamp = 1641010800000; // 时间戳,单位为毫秒
const date = new Date(timestamp);
const zeroTimestamp = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime(); // 当天零点的时间戳
const diff = (timestamp - zeroTimestamp) / 1000; // 时间戳与当天零点时间戳之间的差值,单位为秒
const hour = Math.floor(diff / 3600); // 小时
const minute = Math.floor((diff % 3600) / 60); // 分钟
const second = Math.floor(diff % 60); // 秒
console.log(hour, minute, second);
Copy after login

Summary:

In JavaScript, converting timestamps into time representations in various period units can be achieved by using various methods of the Date object , including toLocaleString(), toLocaleDateString(), toLocaleTimeString(), getFullYear(), etc. At the same time, the time difference can also be calculated to obtain expressions such as hours, minutes, and seconds. The above method can meet most of the time conversion needs, while also giving developers enough freedom to carry out customized development as needed to improve user experience.

The above is the detailed content of javascript timestamp conversion cycle. 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