If you are working with date and times then here are the best way to get dates and time with JS.
You can save a picture for quick reference
1. You can use the toDateString();
method to get the current date with the day,
month and year as the string. Alternatively, you can also get the getDate()
, getMonth()
and getYear()
to get the current date, month and year separately.
2. You can use the toTimeString()
method to get the current time of the
day as a string. Alternatively, you can use the getHours( )
, getMinutes( )
and getSeconds()
method to get the current hour, minute and second
of the day separately.
3. Both toDateString()
and toTimeString()
show the current date and
time without being aware of the locale. If you plan to format the date
and time shown to users based on their locale, you can use the
toLocaleDateString()
and toLocaleTimeString()
methods respectively.
4. If you ever decide that you want to show the universal time to all
your users irrespective of the timezone that they are in, you can use
the toUTCString()
method. JavaScript also allows you to get the UTC
day, month and year using the getUTCDate()
, getUTCMonth()
and
getUTCYear()
methods respectively.