Javascript Timestamp Conversions

Working with timestamps is an essential part of many applications, including tracking user activity and scheduling events. In JavaScript, a timestamp is a numeric value that represents the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC.

Converting Timestamps to Dates

To convert a timestamp to a human-readable date and time format, JavaScript provides the new Date()constructor and two methods:
  • Date.toLocaleString(): returns a string representation of a date and time using the system’s local time zone
  • Date.toUTCString(): returns a string representation of a date and time in UTC
Example:
1 2 3 4 5 6 7const timestamp = 1647022561867; const date = new Date(timestamp); const localTimeString = date.toLocaleString(); const utcTimeString = date.toUTCString(); console.log(localTimeString); // "3/11/2023, 4:36:01 PM" console.log(utcTimeString); // "Fri, 11 Mar 2023 16:36:01 GMT"

Converting Dates to Timestamps

To convert a date and time value to a timestamp, JavaScript provides three methods:
  • Date.getTime(): returns the timestamp value for a Date object
  • Date.parse(): parses a date string and returns a timestamp value
  • Date.UTC(): returns a timestamp value for a specified date and time in UTC
1 2 3 4 5 6 7 8 9const dateString = "March 11, 2023 4:36:01 PM"; const timestamp1 = Date.parse(dateString); const date = new Date(dateString); const timestamp2 = date.getTime(); const timestamp3 = Date.UTC(2023, 2, 11, 16, 36, 1); console.log(timestamp1); // 1647022561000 console.log(timestamp2); // 1647022561000 console.log(timestamp3); // 1647022561000
JavaScript provides several methods for converting timestamps between different formats. Here are the most common types of timestamp conversions in JavaScript with examples:

Unix Timestamp to JavaScript Date Object:

Unix timestamps are the number of seconds that have elapsed since January 1, 1970. To convert a Unix timestamp to a JavaScript Date object, you can use the new Date() constructor and pass the timestamp as an argument. Here's an example:
1 2 3 4const unixTimestamp = 1615426140; const dateObject = new Date(unixTimestamp * 1000); console.log(dateObject); // Output: Fri Mar 10 2021 23:29:00 GMT+0530 (India Standard Time)

JavaScript Date Object to Unix Timestamp:

To convert a JavaScript Date object to a Unix timestamp, you can use the getTime() method to get the number of milliseconds since January 1, 1970, and then divide it by 1000 to get the number of seconds. Here's an example:
1 2 3 4const dateObject = new Date(); const unixTimestamp = Math.floor(dateObject.getTime() / 1000); console.log(unixTimestamp); // Output: 1647015372

JavaScript Date Object to ISO String:

ISO string is a standard format for representing dates and times in a readable format. To convert a JavaScript Date object to an ISO string, you can use the toISOString() method. Here's an example:
1 2 3 4const dateObject = new Date(); const isoString = dateObject.toISOString(); console.log(isoString); // Output: 2022-03-10T18:18:53.689Z

ISO String to JavaScript Date Object:

To convert an ISO string to a JavaScript Date object, you can use the Date.parse() method and pass the string as an argument. Here's an example:
1 2 3 4const isoString = '2022-03-10T18:18:53.689Z'; const dateObject = new Date(Date.parse(isoString)); console.log(dateObject); // Output: Fri Mar 10 2022 23:48:53 GMT+0530 (India Standard Time)

JavaScript Date Object to Local String:

Local string is a string representation of a date and time in the browser's local time zone. To convert a JavaScript Date object to a local string, you can use the toLocaleString() method. Here's an example:
1 2 3 4const dateObject = new Date(); const localString = dateObject.toLocaleString(); console.log(localString); // Output: 3/11/2023, 12:26:53 AM
These are the most common types of timestamp conversions in JavaScript. It's important to understand these conversions to work with dates and times in JavaScript effectively.
© 2024 made by www.epochconverter.io