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.
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);
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);
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);
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() 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());
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'));
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());
Other date object methods
To format the date, we may use methods on the date object such as
getDate(), `getMonth()`, and
const date = new Date(1666632563517); console.log(date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear());
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:
Code language: Bash (bash)
npm install moment
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);
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);
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 ?
Free money-back guarantee
Unlimited access to all platform courses
100's of practice projects included
ChatGPT Based Instant AI Help (Jarvis)
Structured Full-Stack Web Developer Roadmap To Get A Job
Exclusive community for events, workshops
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
Leave a question/feedback and someone will get back to you
- React.js date-picker