Convert Timestamp to Date in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The UNIX timestamps can be converted to the required date and time formats by using the Date() object and its methods in JavaScript.

Introduction

There is no equivalent function strftime in JavaScript, unlike in other languages. However, JavaScript has multiple functions to present date and time in a way that humans can understand.

By using the Date() object and its methods in JavaScript the UNIX timestamps can be converted to the required date and time formats.

There are several ways to obtain the current timestamp of the machine using JavaScript's Date object. Machine timestamp results are calculated by JavaScript and returned in microseconds since 1 January 1970 00:00:00 UTC.

Note Unix time describes a point in time. Since the Unix epoch, it is the number of seconds that have passed. The Unix epoch is 00:00:00 UTC on 1 January 1970.

Get the Current Time in JavaScript

Using the below code we can get the device's current timestamp when creating an object Date in JavaScript without any arguments:

Output

Try the code in your console and get the current time in JavaScript. Below is a sample image of the console.

Current Time

Several Functions To Convert Timestamps to Date and Time in a Different Format that Humans can Read

FunctionDescription
toDateStringA human-readable string is created by converting the date portion of a Date object.
toTimeStringConverts the time portion of a Date object into format humans can understand.
toString Lets you convert a Date object to a string value.
toLocaleStringProduces a string from a Date object using locale conventions.
toLocaleDateStringIt returns the only date of a Date object in string format. The strings are formatted using locale conventions.
toLocaleTimeStringIt returns the only time of a Date object in string format. The strings are formatted using locale conventions
toJSON The date is returned as JSON data in string format
toISOStringReturns a string representation of the date, according to the ISO standard
toUTCString()converts Date objects to strings according to universal time.

Example: Convert Timestamp to Date in JavaScript

Let's implement the functions that we learned above:

Output

Explanation We have already multiplied the Unix timestamp by 1000 to avoid converting seconds to milliseconds in a different step. Since the Date constructor takes milliseconds as a parameter, we have converted seconds to milliseconds.

Conclusion

  • JavaScript uses a Date() object and its methods for all the functions that deal with date and time.
  • In JavaScript, you can get the current timestamp of a machine using the Date object.
  • Since the Date constructor takes milliseconds as a parameter, you must convert the timestamp from seconds to milliseconds.
  • There are several functions to convert timestamps to date and time in a different format that humans can read, for eg: toJSON() - the date is returned as JSON data in string format .