Home > Web Front-end > JS Tutorial > body text

How Can We Convert Milliseconds to a Readable Date in jQuery/JavaScript?

Susan Sarandon
Release: 2024-10-21 17:15:02
Original
486 people have browsed it

How Can We Convert Milliseconds to a Readable Date in jQuery/JavaScript?

Converting Milliseconds to a Date in jQuery/JavaScript

To obtain an accurate timestamp for a message entered in a "shoutbox," you can obtain the milliseconds since January 1, 1970 00:00:00 UTC using the following code:

<code class="js">var time = new Date();
var milliseconds = time.getTime();</code>
Copy after login

This returns a numerical value, such as 1294862756114, representing the time elapsed since the specified epoch.

To convert this milliseconds timestamp into a more legible date format, such as DD/MM/YYYY HH:MM:SS, you can utilize JavaScript's Date constructor and toString() method:

<code class="js">var date = new Date(milliseconds);
console.log(date.toString());</code>
Copy after login

This will output the date in the following format:

Wed Jan 12 2011 12:42:46 GMT-0800 (PST)
Copy after login

This format includes the day of the week, date, month, year, hours, minutes, and seconds, along with the time zone offset.

The above is the detailed content of How Can We Convert Milliseconds to a Readable Date in jQuery/JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!