Bootstrap FreeKB - NodeJS - Date and Time
NodeJS - Date and Time

Updated:   |  NodeJS articles

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

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

 

Which should return something like this.

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

 

toUTCString can be used to return the date and time in UTC / GMT.

const timestamp = () => `${new Date().toUTCString()}`
console.log(timestamp());

 

Which should return something like this.

Mon, 22 May 2023 05:44:22 GMT

 

toISOString can be used to return the date as an ISO string.

const timestamp = () => `${new Date().toISOString()}`
console.log(timestamp());

 

Which should return something like this.

2023-05-22T05:45:58.662Z

 

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

 

 

 




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 3176bb in the box below so that we can be sure you are a human.