Yes, you read it at the right moment. which is the most popular library while struggling with date and time.
I personally used it many times in my projects and many date-picker libraries like ‘RC-date-picker’ depends on the moment. as it takes the moment object as a value and returns the same.
Date() can be quite simple if you know how to deal with it. I’ll walk you through some simple implementations of Date() throughout this blog.
first, let’s take brief knowledge about time zones:
Time zones: –
1) Local Time – It refers to the current time zone in which our computer is right.
2) UTC – It is like GMT – which stands for Greenwich mean time. (Technically both are the same)
How to create a date:
we can create a date with a new Date() method and there are four ways to create a date using a new Date().
- Date string method.
- Passing arguments method.
- Timestamp method.
- No arguments method.
1.) Date string method: –
In this method, you can create a date by passing a date string in the new Date().
Usually, we opt for the date string method because we are using date strings since our childhood.
But here’s a catch, if we provide a string like 2000-03-16 it’s totally fine, but writing it as 16-03-2000 in the Date object, returns an Invalid Date.
let’s see why-
because there are multiple date formats used all over the world. And dates like 05-07-2011 can be 05 July 2011 or 07 May 2011. It is a bit confusing, right?
What do these values mean?
- YYYY -> 4-digit year
- MM -> 2-digit month
- DD -> 2-digit date
- – -> Date delimiters
- T -> Indicates – times start here
- HH -> 24-digit hour
- Mm -> Minutes
- ss -> Seconds
- sss -> Milliseconds
- : -> Time delimiters
- Z: If Z is present, the date will be set to UTC. otherwise, it’ll be Local Time. (Z is only applicable when time is provided.)
Time parameters are optional here if you creating a date. So, if you create a date for 11th June 2019. You can write like this:
Wait! Sorry for the interruption, there’s one more catch. If you reside in a region which is behind GMT you will get the date behind GMT:
If you reside in a region that is ahead of GMT, you will see the date as June 11th.
So, this happens because without time parameters Date object returns the date according to the UTC standard. If you want the date to be in local standard use time parameters too.
Conclusion: The whole matter of local and UTC thing can mess up the code and generate hard-to-catch errors, so the recommendation is not to create a date with the date Strings method.
2.) Passing arguments method: –
In the Date() object you can pass up to 7 arguments. Which are:
- Year: 4-digit year.
- Month: Month of the year (0-11). The month is zero-indexed. Defaults to 0 if left empty.
- Day: Day of the month (1-31). Defaults to 1 if left empty.
- Hour: Hour of the day (0-23). Defaults to 0 if left empty.
- Minutes: Minutes (0-59). Defaults to 0 if left empty.
- Seconds: Seconds (0-59). Defaults to 0 if left empty.
- Milliseconds: Milliseconds (0-999). Defaults to 0 if left empty.
Many developers avoid this approach, I was also unaware of this approach before writing this blog, but this approach is quite easy and accurate.
Reading it from left to right. Arguments are blessings. (*month argument starts from 0. A.k.a. 0 for January.)
Notice here, the dates created are in Local Time.
In case you want the UTC date format. You need to pass the Date.UTC formatter. Ex.:
3.) Timestamp method: –
Well, it is very rare to create dates with epoch time, but we can use it to compare dates.
4.) No arguments method: –
With no arguments passed in the Date object, returns the current date in local Time.
This is the image above; you can tell the time when I wrote this blog.
I hope you understand about the Date object and will not terrify by this cute object again.