In this guide, I will discuss how to get the timestamp in Nodejs in its different forms.
We can use timestamps in our applications to, for instance, inform users about when an activity was performed.
In this guide, I will give you a walkthrough on how to grab the timestamp in Nodejs. We will discuss the different forms of timestamps that we can obtain in a Nodejs application.
Points we cover:
- Understand how to obtain the current date, month, year, hour, minutes, and seconds in Node.js.
- Understand how to obtain the current date-time formatted as YYYY-MM-DD hh:mm:ss, YYYY-MM-DD, DD-MM-YYYY, and so on.
- Understand how to obtain the current timestamp
- Understand how to calculate the date and time from a given timestamp.
The JavaScript Date object is used to handle date and time in Node.js. It is available to us by default, or I must say globally available and does not require module import.
Getting Current Date and Time in YYYY-MM-DD hh:mm:ss Format
The current date and time can be fetched by first creating a new Date object. Thereafter methods of the object can be called to get the date and time values.
// new Date object
const dateObject = new Date();
- getDate – returns the month’s day (1-31)
- getMonth – this function returns the month as an integer (0-11). It is worth noting that January is represented as 0 and December as 11.
- getFullYear – returns the year in four digits.
- getHours – returns the current time in 24-hour format (0-23)
- getMinutes – returns the current minute (0-59)
- getSeconds – returns the number of seconds (0-59)
Using the methods described above, one can create the date and time in the YYYY-MM-DD hh:mm:ss format, or in some other format. Although some methods return single-digit values, we must append 0 before them (for example “6” becomes “06”).
const dateObject = new Date();
// current date
// adjust 0 before single digit date
const date = (`0 ${dateObject.getDate()}`).slice(-2);
// current month
const month = (`0 ${dateObject.getMonth() + 1}`).slice(-2);
// current year
const year = dateObject.getFullYear();
// current hours
const hours = dateObject.getHours();
// current minutes
const minutes = dateObject.getMinutes();
// current seconds
const seconds = dateObject.getSeconds();
// prints date in YYYY-MM-DD format
console.log(`${year}-${month}-${date}`);
// prints date & time in YYYY-MM-DD HH:MM:SS format
console.log(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`);
// prints time in HH:MM format
console.log(`${hours}:${minutes}`);
Let us run the file to check if we have the result correctly:
$ node timestamps.js
2021-11-28
2021-11-28 1:26:5
1:26
Getting Current Timestamp
The Date.now() method can be used to obtain the timestamp. It is important to keep in mind that this method returns the timestamp in milliseconds. We can divide the timestamp by 1000 to get the timestamp in seconds.
const timestamp = Date.now();
// timestamp in milliseconds
console.log(timestamp);
// timestamp in seconds
console.log(Math.floor(timestamp/1000));
Let us run the file in the terminal to see the output:
$ node timestamps.js
1638043140212
1638043140
Getting Date and Time from Timestamp
The timestamp is passed as a parameter to the Date constructor in order to get the date and time values from it.
The above-mentioned methods can then be used to obtain a date and time string.
Because JavaScript timestamps are specified in milliseconds, if the given timestamp is in seconds, you must convert it to milliseconds by multiplying it by 1000.
// current timestamp in milliseconds
const timestamp = Date.now();
const dateObject = new Date(timestamp);
const date = dateObject.getDate();
const month = dateObject.getMonth() + 1;
const year = dateObject.getFullYear();
// prints date & time in YYYY-MM-DD format
console.log(`${year}-${month}-${date}`);
Let us now run the file to check if we get the expected output:
$ node timestamps.js
2021-11-28
Perfect! The codes have turned out great and we have successfully drawn all the right data and time formats.
Conclusion
This guide walks you through the printing timestamps in Nodejs. I have also discussed how we can retrieve the different formats of timestamps. We can use timestamps in our applications to, for instance, inform users about when activity was performed.
This guide contains examples of how you can extract all the different formats of timestamps.