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.
- 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);
- 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);
- 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);
- 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);
- 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);
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!

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool