Although this striking connection with our daily lives,
In this post, I have tried to simplify the
Date object as per my understanding and the most common functionalities I use in day-to-day development. Let us get started...
Ways we can create the
There are four ways we can create
Using empty constructor
Using an empty constructor will create the current date and time. This is the most common way of creating a
Date object. This will create the
Date in the client's local timezone.
I generally use this way of creating the date when adding a new object. Like when creating a new TODO, I pass
createdDate as a
We can use the date constructor to create a Date object based on the number of milliseconds passed since Jan 1st of 1970 UTC+0. Jan 1st of 1970 UTC+0 is considered as Unix Epoch Time.
So if you pass
24 * 60 * 60 * 1000 (which is 24 hours in milliseconds), you will get the date as Jan 2nd of 1970 UTC+0.
In most cases, this is the standard way of creating
Date because it is easier to communicate with the backend. When you want to save or retrieve dates, the backend prefers Unix Epoch Time.
Passing each date entity separately
We can also create a
Date object by passing individual values of year, month, day, hours, minutes, seconds, and milliseconds.
If we omit any values, the
Date constructor will take the default values for those parameters.
I rarely use this method of creating the
Date object. Because I would prefer passing milliseconds to the constructor most of the time.
month Integer value representing the month, beginning with 0 for January to 11 for December.
Passing the date string
The date constructor also accepts a string as a parameter and creates a
Date object by parsing the date string. This I feel is the most dangerous way of creating the date. Because we have to be careful of the format of the date string.
Helpful methods in the
Now that we know how to create the
Date object, let us understand different methods on the date object. These common methods are useful in displaying dates or manipulating the dates.
There are also some static methods on the date constructor that are used as shorthands for common tasks.
getFullYear() returns the year part from the date. With
setFullYear() you can set the year part. Setting the year will not affect any other part of the date.
getMonth() returns month part from the date object. And with its counterpart
setMonth() we can set the month in the date.
getDate() returns the current date and
setDate() sets the date in the date object.
setMilliseconds() are used for getting and setting hours, minutes, seconds and milliseconds respectively.
This method is used to get the current date and time as the number of milliseconds passed since Unix epoch time. The number of milliseconds returned by this method is referred to as the timestamp.
This is the most used method in my personal projects. This timestamp might even serve as id in some cases.
Date to another timezone
Most of us work on applications that are used by a global audience. And it is our duty as web developers to make our content accessible for all users. Localizing the date is a big part of this and it makes for a great user experience. This is why converting dates to different timezones is one of the common tasks we perform on the
Let us understand this through an example.
If you want to read more about
Date.toLocalString(), please check this W3 School link
If you have anything more than what I have covered, comment down below. Until then, Happy Coding!!