Converting Milliseconds to a Readable Date in jQuery/JavaScript
A common requirement in web development is displaying a date or time in a human-readable format. When working with milliseconds, which represent the elapsed time since January 1, 1970 00:00:00 UTC, converting them to a meaningful date format becomes necessary.
To convert milliseconds to a date object in JavaScript, you can use the Date object. Here's how:
<code class="js">var time = new Date(); var milliseconds = time.getTime();</code>
This will give you a variable milliseconds containing a number like 1294862756114. To convert this to a readable date, create a new Date object using the milliseconds as an argument:
<code class="js">var date = new Date(milliseconds);</code>
Now, you can use the toString() method to get a string representation of the date in the following format:
Wed Jan 12 2011 12:42:46 GMT-0800 (PST)
This will display the day of the week, month, day, year, hours, minutes, seconds, and timezone offset from UTC.
Example Usage:
In your shoutbox code, you can use the following code to convert the milliseconds to a readable date:
<code class="js">var time = new Date().getTime(); var date = new Date(time); console.log(date.toString());</code>
This will output the time the message was entered in a human-readable format.
The above is the detailed content of How to Convert Milliseconds to a Readable Date in jQuery/JavaScript?. For more information, please follow other related articles on the PHP Chinese website!