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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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 .
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more