NodeJS Timestamp Conversions

Timestamp conversions are a common task when working with date and time in Node.js. Fortunately, Node.js provides several libraries for this purpose, and one of the most popular is dayjs.

dayjs is a lightweight and fast library for parsing, validating, manipulating, and formatting dates and times. It is similar to the well-known moment.js library, but with a smaller bundle size and faster performance.

Here are some examples of how to use dayjs for timestamp conversions in Node.js:

Convert a timestamp to a formatted date and time string:

1 2 3 4 5 6 7const dayjs = require('dayjs'); const timestamp = 1647144063000; // March 12, 2022 10:41:03 AM UTC const formattedDate = dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss'); console.log(formattedDate); // Output: "2022-03-12 10:41:03"
In this example, dayjs(timestamp) creates a dayjs object from the given timestamp, and format('YYYY-MM-DD HH:mm:ss') formats it into a string that represents the date and time in the desired format.

Convert a date and time string to a timestamp:

1 2 3 4 5 6 7const dayjs = require('dayjs'); const dateString = '2022-03-12 10:41:03'; const timestamp = dayjs(dateString).valueOf(); console.log(timestamp); // Output: 1647144063000
Here, dayjs(dateString) creates a dayjs object from the given date and time string, and valueOf() returns the timestamp in milliseconds.

Convert a timestamp to a different time zone:

1 2 3 4 5 6 7 8 9 10 11 12const dayjs = require('dayjs'); const utc = require('dayjs/plugin/utc'); const timezone = require('dayjs/plugin/timezone'); dayjs.extend(utc); dayjs.extend(timezone); const timestamp = 1647144063000; // March 12, 2022 10:41:03 AM UTC const convertedTimestamp = dayjs(timestamp).tz('America/New_York').valueOf(); console.log(convertedTimestamp); // Output: 1647128463000
In this example, dayjs(timestamp).tz('America/New_York') converts the timestamp to the America/New_York time zone and returns a dayjs object in that time zone. valueOf() then returns the timestamp in milliseconds.

These are just a few examples of how dayjs can be used for timestamp conversions in Node.js. dayjs provides many more features for manipulating and formatting dates and times, and it's worth exploring its documentation to learn more.

Converting a Unix Timestamp to a Date String:

The Unix timestamp represents the number of milliseconds since January 1, 1970, UTC. To convert a Unix timestamp to a date string, we can use the dayjs library's format() method. Here's an example:
1 2 3 4 5 6 7const dayjs = require('dayjs'); const unixTimestamp = 1647313260000; // March 15, 2022 12:21:00 AM UTC const dateString = dayjs(unixTimestamp).format('YYYY-MM-DD HH:mm:ss'); console.log(dateString); // Output: '2022-03-15 00:21:00'
In this example, we create a dayjs object from the Unix timestamp and then use the format() method to format the date in the desired format.

Converting a Date String to a Unix Timestamp:

To convert a date string to a Unix timestamp, we can use the dayjs library's valueOf() method. Here's an example:
1 2 3 4 5 6 7const dayjs = require('dayjs'); const dateString = '2022-03-15 00:21:00'; const unixTimestamp = dayjs(dateString).valueOf(); console.log(unixTimestamp); // Output: 1647313260000
In this example, we create a dayjs object from the date string and then use the valueOf() method to get the Unix timestamp in milliseconds.

Converting a Date String to Another Timezone:

To convert a date string to another timezone, we can use the dayjs library's tz() method. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12const dayjs = require('dayjs'); const utc = require('dayjs/plugin/utc'); const timezone = require('dayjs/plugin/timezone'); dayjs.extend(utc); dayjs.extend(timezone); const dateString = '2022-03-15 00:21:00'; const convertedDate = dayjs(dateString).tz('America/New_York').format('YYYY-MM-DD HH:mm:ss'); console.log(convertedDate); // Output: '2022-03-14 20:21:00'
In this example, we create a dayjs object from the date string and then use the tz() method to convert it to the America/New_York timezone. Finally, we use the format() method to format the date in the desired format.

Converting a Date Object to a Date String:

To convert a Date object to a date string, we can use the dayjs library's format() method. Here's an example:
1 2 3 4 5 6 7const dayjs = require('dayjs'); const dateObject = new Date('2022-03-15T00:21:00.000Z'); const dateString = dayjs(dateObject).format('YYYY-MM-DD HH:mm:ss'); console.log(dateString); // Output: '2022-03-15 00:21:00'
In this example, we create a dayjs object from the Date object and then use the format() method to format the date in the desired format.

These are the four types of timestamp conversions that can be performed using dayjs in Node.js.
© 2024 made by www.epochconverter.io