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

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

DDD
Release: 2024-10-21 16:28:30
Original
483 people have browsed it

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

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>
Copy after login

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>
Copy after login

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)
Copy after login

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>
Copy after login

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!

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
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!