Home > Web Front-end > Front-end Q&A > How to convert and format time in Node.js

How to convert and format time in Node.js

PHPz
Release: 2023-04-05 10:38:26
Original
3615 people have browsed it

Node.js is an open source tool for writing server-side code using JavaScript. In Node.js, time processing and formatting are very common operations. In this article, we will explore how to convert and format time in Node.js.

Timestamp

When writing Node.js code, you usually encounter many timestamp operations. A Unix timestamp refers to the number of seconds since January 1, 1970 00:00:00 UTC. In Node.js, you can use the following code to get the current timestamp:

const timestamp = Math.floor(Date.now() / 1000);
console.log(timestamp);
Copy after login

This example uses Date.now() to get the number of milliseconds of the current time, then uses Math.floor() to round down, and Divide the value by 1000 to convert it to a timestamp in seconds.

Time formatting

In Node.js programming, you may need to convert time to a specific format, for example:

  1. Convert time to GMT format.
  2. Convert the time to the format of your time zone.
  3. Convert time to other formats such as ISO format, for example 2022-08-12T17:28:00 00:00.

To format time in Node.js, you can use Moment.js or the Node.js built-in Intl.DateTimeFormat() function.

Moment.js Time Conversion

Moment.js is a popular JavaScript library for parsing, validating, manipulating, and formatting dates and times. To use Moment.js with Node.js, you need to first install Moment.js and import it in your code. You can install Moment.js using the following command:

npm install moment
Copy after login

Then, use the following code in your code to format the time:

const moment = require('moment');

const date = moment();
console.log(date.format('YYYY-MM-DD hh:mm:ss'));
Copy after login

In this example, we create using the moment() function Moment object of the current time. We then use the format() function to format the date into a date-time string, such as 2022-08-12 05:28:00.

Node.js Intl.DateTimeFormat()

If you don’t want to use an additional library, you can format the time using Node.js’ built-in Intl.DateTimeFormat() function. Using this function is a little more cumbersome, but it's native and requires no additional libraries to be installed. For example:

const date = new Date();
const formatter = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  timeZone: 'America/New_York',
});
console.log(formatter.format(date));
Copy after login

In this example, we use the new Date() function to create a Date object of the current time. We then create an Intl.DateTimeFormat() object and pass it the required options. Then we use this formatter manager to format the date object. You can get a time in a format similar to 08/12/2022, 01:28:00 PM.

Summary

Here, we have discussed how to do timestamp and time formatting operations in Node.js. Whether you use Moment.js or the built-in Intl.DateTimeFormat() function of Node.js, there are corresponding solutions to choose from for time conversion and formatting in Node.js. If you need to work with dates and times in Node.js, use these tips whenever possible.

The above is the detailed content of How to convert and format time in Node.js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template