Home  >  Article  >  Web Front-end  >  How Can We Convert Milliseconds to a Readable Date in jQuery/JavaScript?

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

Susan Sarandon
Susan SarandonOriginal
2024-10-21 17:15:02487browse

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>

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>

This will output the date in the following format:

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

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!

Statement:
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