Updated April 3, 2023
Introduction to Node.js timestamp
A timestamp is the numerical conversion of date and time value in an Integer like a number that is the time in milliseconds. Every technology and in every application, the most widely used functionality is the date object, which helps retrieve, share, save, and update the date values across the application. In NodeJS Timestamp, the Date and time fields come under the JavaScript Framework, where the Date Object of JavaScript is used without any external dependency and returns back time in milliseconds. In this topic, we are going to learn about Node.js timestamps.
Syntax
Get Current Timestamp Value in the millisecond
var dateTimeStamp = Date.now();
console.log(" Current Timestamp :: " + dateTimeStamp);
Output:
How setTimeout Function Works in Node.js?
In NodeJS, Timestamp objects consisting of Date and Time fields are provided by the JavaScript framework itself and need not be imported explicitly in NodeJS code. As soon as JavaScript executes the Date.now() method, it calculates the Total number of Milliseconds which have been passed since Jan 1, 1970, as it’s the UNIX timestamp and picks the system timezone as default timezone and returns timestamp in milliseconds. It has been observed that Date.now() is not compatible with IE 8 or any lower versions, and so you might have created a new Date object and call getTime method instead of calling Date.now(). The JavaScript Timezone comes with lots of inbuilt methods which can be used in applications using JavaScript Framework. Playing around with time and date values and utilizing these values in UI fields, storing in the database or comparing time or Date across the application is one of the most common use cases in any application.
Examples of Node.js timestamp
Given below are the examples of Node.js timestamp:
Example #1 – Get Current Timestamp Value in the millisecond
Code:
var dateTimeStamp = Date.now();
console.log(" Current Timestamp :: " + dateTimeStamp);
Output:
Example #2 – Get Time in milliseconds from the specified Timestamp
Code:
var dateTimeStamp = Date.now();
var dateObject = new Date(dateTimeStamp);
var timeInMs = dateObject.getTime();
console.log("Date Time in milliseconds :: " + timeInMs);
Output:
Example #3 – Get Today’s Date Value from the specified Timestamp
Code:
var dateTimeStamp = Date.now();
var dateObject = new Date(dateTimeStamp);
var dateFromTS = dateObject.getDate();
console.log("Today's Date Value :: " + dateFromTS);
Output – Today’s Date Value:: 11
Note: Date is returned as an integer ranging from 1-31
Example #4 – Get Month Value from the specified Timestamp
Code:
var dateTimeStamp = Date.now();
var dateObject = new Date(dateTimeStamp);
var monthFromTS = dateObject.getMonth() + 1;
console.log("Month Value is :: " + monthFromTS);
Output – Month Value is:: 8
Note: Month is returned as an integer ranging from 0-11, where 0 is for January and 11 for December. Make sure to account for this while displaying or using Month value.
Example #5 – Get Today’s Year in 4 digit format from the specified Timestamp
Code:
var dateTimeStamp = Date.now();
var dateObject = new Date(dateTimeStamp);
var yearFromTS = dateObject.getFullYear();
console.log("Year in 4 digit Value :: " + yearFromTS);
Output:
Note: Year Value is returned as a 4 digit Integer always
Example #6 – Get Timestamp in Seconds
Code:
var timeInMS = Date.now();
var timeInSeconds = Math.floor(timeInMS/1000)
console.log("Timestamp in MS :: " + timeInMS);
console.log("Timestamp in Seconds :: " + timeInSeconds);
Output:
Example #7 – Get-Date in DD-MM-YYYY format from the current timestamp
Code:
var dateTimeStamp = Date.now();
var dateObject = new Date(dateTimeStamp);
var yearFromTS = dateObject.getFullYear();
var monthFromTS = dateObject.getMonth() + 1;
var dateFromTS = dateObject.getDate();
console.log("Date in DD/MM/YYYY :: " + dateFromTS + "/" + monthFromTS + "/" + yearFromTS);
Output:
Example #8 – Get-Date in UTC from the current timestamp
Code:
var dateTimeStamp = 1597049017329;
var dateObject = new Date(dateTimeStamp);
var utcYearFromTS = dateObject.getUTCFullYear();
var utcMonthFromTS = dateObject.getUTCMonth() + 1;
var utcDateFromTS = dateObject.getUTCDate();
var utcTimeFromTS = dateObject.getUTCHours();
var utcMinutesFromTS = dateObject.getUTCMinutes();
console.log("UTC Date in DD/MM/YYYY HH:MM " + utcDateFromTS + "/"
+ utcMonthFromTS + "/" + utcYearFromTS + " " + utcTimeFromTS + "hrs : " + utcMinutesFromTS + "minutes");
Output:
Example #9 – Get Offset in Timezone from Specified Timestamp
Code:
var dateTimeStamp = Date.now();
var dateObject = new Date(dateTimeStamp);
var timeZoneOffsetValue = dateObject.getTimezoneOffset();
console.log("Offset :: " + timeZoneOffsetValue);
Output:
Note: The offset in a timezone is the difference between the system timezone (Local timezone) and the UTC timezone, and the value is returned in minutes
Other than these, more methods come along with the Date timestamp object in JavaScriptDate API, such as getDay, getHours, getMilliseconds, getTimezoneOffset etc.
Advantages
Every technology and application has the requirement of using Date and Time values, and so does it in NodeJS applications. Fetching timestamp details in NodeJS doesn’t require any additional imports or has no external dependency as it’s wrapped along with JavaScript Framework. It easy to use as the syntax is as easy as a single line of code. Comparing the date along with the time object is just as simple as comparing the 2 integer numbers. The reason is that the Timestamp in JavaScript always returns integer numbers in milliseconds format. Also with this fetching time based on UTC timezone and getting the offset values is also another advantage.
Conclusion
Coming to an end to this Module, where we discussed how we could fetch date, time, day, year, month, and many more Time-related values in NodeJS from the timestamp. Also how Timestamp in NodeJS comes along with the JavaScript framework and is easy to use. So just keep playing around the date-time objects and be a perfect developer to develop any complex logic around date time.
Recommended Articles
This is a guide to Node.js timestamp. Here we discuss How setTimeout function works in Node.js along with the Examples. You may also have a look at the following articles to learn more –