DEV Community

Cover image for Mastering Date & Time Manipulation in JavaScript (Node.js)
Srashti Gupta
Srashti Gupta

Posted on

2

Mastering Date & Time Manipulation in JavaScript (Node.js)

Introduction

In JavaScript (and Node.js), managing dates and times is a common and crucial task. Whether you are building a time-sensitive application or handling user data with expiration timestamps, understanding how to manipulate and compare dates is essential.

In this article, we’ll explore several JavaScript Date methods that can help you handle timestamps, compare expiration times, and work with dates effectively.


Understanding JavaScript Date Methods

JavaScript provides several ways to work with dates and times. Below are some of the most useful methods you will frequently use when working with date and time values.

1. Date.now()

The Date.now() method returns the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 UTC (also known as the Unix Epoch).

Example:

const currentMilliseconds = Date.now();
console.log(currentMilliseconds);
// Example Output: 1678595739816
Enter fullscreen mode Exit fullscreen mode

This method is often used when you need the current timestamp, and it's fast since it doesn’t require creating a Date object.

2. Date.parse()

The Date.parse() method takes a date string as input and returns the number of milliseconds since the Unix Epoch.

Example:

const timeInMillis = Date.parse('2025-03-12T00:00:00Z');
console.log(timeInMillis);
// Example Output: 1678598400000
Enter fullscreen mode Exit fullscreen mode

This is particularly useful for converting a string representation of a date into a timestamp.

3. Date.UTC()

The Date.UTC() method accepts year, month, day, hours, minutes, and seconds as arguments and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Example:

const utcTime = Date.UTC(2025, 2, 12); // Note: Month is 0-indexed, so 2 is March
console.log(utcTime);
// Example Output: 1678588800000
Enter fullscreen mode Exit fullscreen mode

This is useful for working with dates in the UTC time zone.


Instance Methods on Date Objects

Once you create a Date object, you can use several instance methods to extract different components of the date, compare times, and manipulate them.

4. getDate()

The getDate() method returns the day of the month from a given Date object.

Example:

const date = new Date('2025-03-12');
console.log(date.getDate()); // Output: 12
Enter fullscreen mode Exit fullscreen mode

5. getDay()

The getDay() method returns the day of the week as a number from 0 to 6, where 0 represents Sunday.

Example:

const date = new Date('2025-03-12');
console.log(date.getDay()); // Output: 3 (Wednesday)
Enter fullscreen mode Exit fullscreen mode

6. getFullYear()

The getFullYear() method returns the full year from the Date object.

Example:

const date = new Date('2025-03-12');
console.log(date.getFullYear()); // Output: 2025
Enter fullscreen mode Exit fullscreen mode

7. getHours()

The getHours() method returns the hour of the day (0-23) from the given Date object.

Example:

const date = new Date('2025-03-12T10:45:00Z');
console.log(date.getHours()); // Output: 10
Enter fullscreen mode Exit fullscreen mode

8. getMilliseconds()

The getMilliseconds() method returns the milliseconds (0-999) of the Date object.

Example:

const date = new Date('2025-03-12T10:45:00.456Z');
console.log(date.getMilliseconds()); // Output: 456
Enter fullscreen mode Exit fullscreen mode

9. getMinutes()

The getMinutes() method returns the minutes (0-59) of the Date object.

Example:

const date = new Date('2025-03-12T10:45:00Z');
console.log(date.getMinutes()); // Output: 45
Enter fullscreen mode Exit fullscreen mode

10. getTime()

The getTime() method returns the time (in milliseconds) since January 1, 1970, 00:00:00 UTC for the Date object.

Example:

const date = new Date('2025-03-12');
console.log(date.getTime()); // Output: 1678588800000
Enter fullscreen mode Exit fullscreen mode

Comparing Dates and Expiration Times

Often in applications, you'll need to check if a certain date or timestamp has expired. This can be achieved by comparing the current time with the expiration time.

Example: Checking if a token has expired

const expirationTime = new Date('2025-03-12T12:00:00Z').getTime();
const currentTime = Date.now();

if (currentTime > expirationTime) {
    console.log('The token has expired');
} else {
    console.log('The token is still valid');
}
Enter fullscreen mode Exit fullscreen mode

In this example, we compare the currentTime (from Date.now()) with a predefined expirationTime to determine if the token has expired.


Conclusion

In this article, we've covered how to handle dates and times in JavaScript using a variety of methods. From simple timestamps to more complex date manipulations, these methods will help you work with dates effectively, whether you’re checking expiration times or dealing with time-sensitive data in your Node.js applications.

Feel free to experiment with these methods, and let me know if you have any questions or need further examples!


AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay