Bootstrap FreeKB - Node.js - Date and Time
Node.js - Date and Time

Updated:   |  Node.js articles

Here is an example of how to return the current date and time.

const timestamp = new Date()
console.log(timestamp);

 

Which should return something like this.

Mon May 22 2023 00:41:47 GMT-0500 (Central Daylight Time)

 

Here are some of the ways to format the Date into a particular format.

const toUTCString        = new Date().toUTCString()
const toDateString       = new Date().toDateString()
const toGMTString        = new Date().toGMTString()
const toISOString        = new Date().toISOString()
const toLocaleDateString = new Date().toLocaleDateString()
const toLocaleTimeString = new Date().toLocaleTimeString()
const toLocaleString     = new Date().toLocaleString()
const toString           = new Date().toString()
const toTimeString       = new Date().toTimeString()
const toJSON             = new Date().toJSON()

console.log(`toUTCString        = ${toUTCString}`)
console.log(`toDateString       = ${toDateString}`)
console.log(`toGMTString        = ${toGMTString}`)
console.log(`toISOString        = ${toISOString}`)
console.log(`toLocaleDateString = ${toLocaleDateString}`)
console.log(`toLocaleTimeString = ${toLocaleTimeString}`)
console.log(`toLocaleString     = ${toLocaleString}`)
console.log(`toString           = ${toString}`)
console.log(`toTimeString       = ${toTimeString}`)
console.log(`toJSON             = ${toJSON}`)

 

Or like this.

now = new Date()

const toUTCString        = now.toUTCString()
const toDateString       = now.toDateString()
const toGMTString        = now.toGMTString()
const toISOString        = now.toISOString()
const toLocaleDateString = now.toLocaleDateString()
const toLocaleTimeString = now.toLocaleTimeString()
const toLocaleString     = now.toLocaleString()
const toString           = now.toString()
const toTimeString       = now.toTimeString()
const toJSON             = now.toJSON()

console.log(`toUTCString        = ${toUTCString}`)
console.log(`toDateString       = ${toDateString}`)
console.log(`toGMTString        = ${toGMTString}`)
console.log(`toISOString        = ${toISOString}`)
console.log(`toLocaleDateString = ${toLocaleDateString}`)
console.log(`toLocaleTimeString = ${toLocaleTimeString}`)
console.log(`toLocaleString     = ${toLocaleString}`)
console.log(`toString           = ${toString}`)
console.log(`toTimeString       = ${toTimeString}`)
console.log(`toJSON             = ${toJSON}`)

 

Which should return something like this.

toUTCString        = Sat, 08 Feb 2025 08:30:36 GMT
toDateString       = Sat Feb 08 2025
toGMTString        = Sat, 08 Feb 2025 08:30:36 GMT
toISOString        = 2025-02-08T08:30:36.098Z
toLocaleDateString = 2/8/2025
toLocaleTimeString = 8:30:36 AM
toLocaleString     = 2/8/2025, 8:30:36 AM
toString           = Sat Feb 08 2025 08:30:36 GMT+0000 (Coordinated Universal Time)
toTimeString       = 08:30:36 GMT+0000 (Coordinated Universal Time)
toJSON             = 2025-02-08T08:30:36.164Z

 

process.env.TZ can be used to specify a timezone.

process.env.TZ = 'US/Central'
now = new Date()

const toUTCString        = now.toUTCString()
const toDateString       = now.toDateString()
const toGMTString        = now.toGMTString()
const toISOString        = now.toISOString()
const toLocaleDateString = now.toLocaleDateString()
const toLocaleTimeString = now.toLocaleTimeString()
const toLocaleString     = now.toLocaleString()
const toString           = now.toString()
const toTimeString       = now.toTimeString()
const toJSON             = now.toJSON()

console.log(`toUTCString        = ${toUTCString}`)
console.log(`toDateString       = ${toDateString}`)
console.log(`toGMTString        = ${toGMTString}`)
console.log(`toISOString        = ${toISOString}`)
console.log(`toLocaleDateString = ${toLocaleDateString}`)
console.log(`toLocaleTimeString = ${toLocaleTimeString}`)
console.log(`toLocaleString     = ${toLocaleString}`)
console.log(`toString           = ${toString}`)
console.log(`toTimeString       = ${toTimeString}`)
console.log(`toJSON             = ${toJSON}`)

 

Which should return something like this. Notice the Locale modifiers are in the US/Central time zone in this example.

toUTCString        = Sat, 08 Feb 2025 08:39:20 GMT
toDateString       = Sat Feb 08 2025
toGMTString        = Sat, 08 Feb 2025 08:39:20 GMT
toISOString        = 2025-02-08T08:39:20.440Z
toLocaleDateString = 2/8/2025
toLocaleTimeString = 2:39:20 AM
toLocaleString     = 2/8/2025, 2:39:20 AM
toString           = Sat Feb 08 2025 02:39:20 GMT-0600 (Central Standard Time)
toTimeString       = 02:39:20 GMT-0600 (Central Standard Time)
toJSON             = 2025-02-08T08:39:20.440Z

 

replace can be used to replace the letter "T" with a space and the trailing period with nothing.

const timestamp = `${new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')}`
console.log(timestamp);

 

Which should return something like this.

2023-05-22 05:45:58

 

strftime (string format time) can be used to format the date time as a string. The npm list command can be used to determine if you have the strftime package installed. If not, the npm install command can be used to install the package. Once installed, your package.json should include the strftime package.

npm install strftime

 

If your package.json file does not have "type": "module" this should mean you are using CommonJS. In this scenario, you would use "require" to import strftime.

const strftime = require('strftime');

 

If your package.json file has "type": "module" this should mean you are using an ES Module. In this scenario, you would "import" strftime.

import strftime from 'strftime';
const now = new Date()
console.log(`now = ${now}`)
const formattedDate = strftime('%Y-%m-%d %H:%M:%S', now);
console.log(`formattedDate = ${formattedDate}`)

 

Following are commonly used options.

 

OptionDescriptionExample
%YYear2018
%yYear18
%mMonth01
%bMonthJan
%BMonthJanuary
%aShort day of the weekSun
%AFull day of the weekSunday
%dDay (with leading zero)04
%-dDay (without leading zero)4
%HHour (24 hour format)22
%IHour (12 hour format)10
%MMinute56
%SSecond23
%sMicroseconds1677062390
%fMicroseconds508159
%pAM or PMAM
%Pam or pmpm
%ZTimezoneCoordinated Universal Time
%zTimezone+0500

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f8adcf in the box below so that we can be sure you are a human.