How to convert timestamp to date in JavaScript?
If you have worked with backend servers and databases, you might know that mainly these backend servers store the date (and time) as a string containing a timestamp. The front-end application may need to display these dates in a proper format. But since these dates are in the form of a timestamp, we must know how to convert these timestamps into an appropriate format of date, taking into account the user’s timezone. Doing this is pretty straightforward with JavaScript. Alternatively, we may use some popular open-source libraries to make things even more straightforward.
This article will look at several techniques for converting a string containing the timestamp information into a proper date format. As a bonus, we’ll go through a couple of popular open-source libraries that we primarily use for this purpose.
Converting timestamps to date formats in JavaScript
JavaScript provides an API for working with dates and times, which we can access via the Date
constructor. It produces a date object with numerous parsing and formatting methods. Using the Date
constructor, we can build a date object as follows:
const date = new Date();
console.log(date);
Code language: JavaScript (javascript)
Alternatively, we may also use the Date.now()
function instead. It produces a timestamp value reflecting the amount of milliseconds since the 1st of January, 1970.
const date = Date.now();
console.log(date);
Code language: JavaScript (javascript)
We may optionally pass a few options to the Date
constructor when building a date object. These parameters can be a timestamp, a date string, a date object, or particular date and time components such as day, month, year, and so on.
We may optionally supply a timestamp to the Date
constructor when producing the current date. As a result, a string comprising the date, time, and timezone is returned.
const date = new Date(1666632563517);
console.log(date);
Code language: JavaScript (javascript)
Formatting the date
Using the date object’s methods, we can format the produced date. Let’s have a look at some of the strategies accessible one by one.
Date.toDateString()
Date.toDateString()
returns the date in a compressed format. To use this approach, we will generate the proper date using the timestamp supplied and then convert the date to a simple format.
const date = new Date(1666632563517);
console.log(date.toDateString());
Code language: JavaScript (javascript)
Date.toLocaleDateString()
We obtain the date in the format of the user’s timezone by using the Date.toLocaleDateString()
function. We may also pass extra arguments to style the date according to language requirements.
const date = new Date(1666632563517);
console.log(date.toLocaleDateString('en-US'));
Code language: JavaScript (javascript)
Date.toLocaleString()
Date.toLocaleString()
functions in the same way as Date.toLocaleDateString()
does. The key distinction between the two is that, in contrast to the Date.toLocaleDateString()
function, which creates the date, the Date.toLocaleString()
method also generates the time.
const date = new Date(1666632563517);
console.log(date.toLocaleString());
Code language: JavaScript (javascript)
Other date object methods
To format the date, we may use methods on the date object such as getDate()
, `getMonth()`, and getFullYear()
.
const date = new Date(1666632563517);
console.log(date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear());
Code language: JavaScript (javascript)
Converting timestamps to date formats in JavaScript using Moment.js
Moment.js is a JavaScript library that got released in 2011. It is used primarily for working with dates in JavaScript. It contains various methods for parsing and validating dates in JavaScript. Also, it has a very developer-friendly API for working with dates.
In JavaScript, we can use this library to convert and format the timestamp value into a valid date format. We will utilize the moment.unix()
method to do this. It takes a timestamp as an argument and outputs a date object.
We may install it using the NPM package manager with the following command:
npm install moment
Code language: Bash (bash)
Alternatively, we can also use a CDN by including a <script>
tag in our HTML document.
We write the following code to convert the timestamp value to a date object.
const date = moment.unix("1666632563");
console.log(date);
Code language: JavaScript (javascript)
We may format the generated date by chaining the format()
method. It accepts a string, containing the date format, as an argument. We will use the following code to format the created date.
const date = moment.unix("1666632563").format("DD/MM/YYYY");
console.log(date);
Code language: JavaScript (javascript)
Conclusion
In this article, we looked at several methods for converting timestamps into the appropriate date and time format. Additionally, we looked at a couple of popular open-source libraries that we primarily use for this purpose.
Thank you so much for reading ?
Sharing is caring
Did you like what Varun Tiwari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: