Date and time

Introduction

Let's talk about Date. It is a built-in Javascript object, and it stores date, time, and provides methods for managing date/time.

It can be used to store creation/modification time, measure time, or simply just print out the current time.

How to create it then?

Using the Date() method, we can create the Date object. Let us look at the example below:

let date = new Date();
console.log(date); //This will log in the current date along with the time too.

The Date() method accepts certain arguments that can be used to create a custom date object with provided input values. Those arguments are:

  • year (YYYY - 4 digits)

  • month (MM - 2 digits from 0 - 11 with 0 being the first month and 11 being the last month)

  • date (DD - 2 digits from 1 - 31)

  • hours (HH - 2 digits)

  • minutes (MM - 2 digits)

  • seconds (SS - 2 digits)

  • milliseconds (SSS - 3 digits)

Let us look at an example now:

let date = new Date(2011, 0, 1, 0, 0, 0, 0);
console.log(date); // 2011-01-01T00:00:00.000Z

let newDate = new Date(2011, 0, 1);
console.log(newDate); // 2011-01-01T00:00:00.000Z
//this would provide the same result as well. The values not provided are assumed to be 0.

Accessing Date components using certain methods

There are certain methods available which can be used to access the date components.

  • getFullYear() - Gets the year

  • getMonth() - Gets the month

  • getDate()- Gets the date

  • getDay()- Gets the day of the week, with 0 being the first day of the week (Sunday), and 6 being the last (Saturday)

  • getHours()- Gets the hours

  • getMinutes()- Gets the minutes

  • getSeconds() - Gets the seconds

  • getMilliseconds() - Gets the milliseconds

Let us look at the example below to understand in depth:

let date = new Date();
console.log(date.getFullYear()); //2023
console.log(date.getMonth()); //0
console.log(date.getDate()); //10
console.log(date.getHours()); //13
console.log(date.getDay()); //2
console.log(date.getMinutes()); //36

console.log(date.getSeconds()); //29
console.log(date.getMilliseconds()); //55

There are two more methods:

  • getTime() - which gets the timestamp for the date. It returns the number of milliseconds passed since 1st January 1970.

  • getTimezoneOffset() - Returns the difference in UTC and local time zone in minutes.

let date = new Date();
console.log(date.getTime()); //1673358063750
console.log(date.getTimezoneOffset()); //0

Setting Date Components

As there are methods that can be used to access date components, there are methods that can allow us to update or set date components too.

  • setFullYear(year, [month], [date])

  • setMonth(month, [date])

  • setDate(date)

  • setHours(hours, [minutes], [seconds], [milliseconds])

  • setMinutes(minutes, [seconds], [milliseconds])

  • setSeconds(seconds, [milliseconds])

  • setMilliseconds(milliseconds)

  • setTime(milliseconds)

P.S. All arguments mentioned within [ ] are optional.

Everyone of these methods has a UTC counterpart except the setTime() method.

Let us look at the example below:

let date = new Date();
console.log(date); //2023-01-10T14:15:49.235Z

date.setFullYear(2024);
date.setMonth(2);
date.setDate(1);
date.setHours(18);
date.setMinutes(18);
date.setSeconds(18);
date.setMilliseconds(118);

console.log(date); //2024-03-01T18:18:18.118Z

Autocorrection

A handy feature that Date objects come with is that while creating a date object, if you end up setting an out-of-range value, then it will auto-adjust itself. Let us look at an example to understand more about it.

let date = new Date(2024, 0, 32); //take a look at the date - it's 32!
console.log(date); //2024-02-01T00:00:00.000Z
// but the output is auto-adjusted. 32 is interpreted as February 1st, 2024

Date.now()

Date.now() returns the current timestamp just like the getTime() method does. However, with Date.now() we don't need to create the Date object.

It is faster, doesn't put pressure on garbage collection ( if you don't know what garbage collection, then be sure to check my other blog on garbage collection in Javascript ).

It is used mostly for convenience.

let date = Date.now();
console.log(date); //1673360650625 - in milliseconds

Date.parse(str)

We can use the Date.parse() to read a date out of a string.

The format of the string is:

"YYYY-MM-DD-THH:MM:SS.SSSZ"

T - It is a delimiter

Z - is optional and denotes the time zone in the format +-HH:MM. A single letter Z means UTC+0.

let date = Date.parse("2012-01-26T13:51:50.417-07:00");
console.log(date); //1327611110417

Date to number conversion and difference between dates

Date can be converted to number which will return a timestamp in milliseconds. Let us look at an example to understand how to do that.

let date = new Date();
console.log(+date); //1673361529705

Also, we can subtract two dates, and the result will be in milliseconds.

let start = Date.now();

for (let i = 0; i < 100000; i++) {
  let doSomething = i * i * i;
}

let end = Date.now();

let result = end - start;
console.log(result); //29

Links

I have attached a link below where you can find the implementation of all the methods mentioned in this blog on Github.

All about Date and Time

Final thoughts

That's it, folks. There wasn't much to explain in this blog except to introduce you to Date. Here's a joke for you my lovely readers.

Girl: When should we go for a dinner date?

Guy: Date.now()

🤣🤣🤣

Stay tuned for more.

You can follow me on Github, LinkedIn.

Thank you for reading.

0
Subscribe to my newsletter

Read articles from Nikhil Kumar Patra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nikhil Kumar Patra
Nikhil Kumar Patra

Web Development fanatic, excited about learning anything related to the same, from the basics to the advanced. I am a final year undergraduate at the International Institute of Information Technology, Bhubaneswar. I like to solve problems and learn about complex algorithms, and new technologies, and I aspire to create an impact in the world of Computer Science someday ❤️. Apart from Web Development, I am currently exploring and learning about iOS App development.