Search This Blog

2024/04/03

Javascript:Date

Without an argument, we have seen that the new Date() constructor
returns the current date and time. But it returns a long date-time
representation.
e.g.
const date = new Date();

we can use methods like getFullYear(), getMonth(), and getDate() to
get the current year, month, and date, respectively.It is important
to note that getMonth() return month in 0 based index so to get actual
month ,we need to add 1 to its value.
e.g.
let fullYear = date.getFullYear();
let month = date.getMonth() +1;
let day = date.getDate();

The Date.now() static method returns the current timestamp in milliseconds.
This is the local time elapsed since the epoch.
e.g.
let dt = Date.now()

Date constructor can create a Date object by passing the timestamp milliseconds
as an argument. Hence, we can pass the Date.now() to the Date constructor to
get the current date and time.
e.g.
let dtNow = new Date(Date.now())


The toJSON() method of the Date object returns the current date and time in
the date time string format. That is YYYY-MM-DDTHH:mm:ss.sssZ.
e.g.
let dtJson = new Date().toJSON();

To extract only date part from datetime we use slice() on toJSON(),date
returned is in format YYYY-MM-DD
e.g.
let dateOnly =new Date().toJSON().slice(0, 10);

toLocaleDateString() method:
To get the current date into a JavaScript string we use
toLocalDateString() method from the Date object.
e.g.
const date = new Date().toLocaleDateString();

You can pass the locale as an argument to the toLocalDateString()
method to get a locale-specific date. For example, to get the current
date in the German locale, you can pass de-DE as the argument.
e.g.
const date = new Date().toLocaleDateString("de-DE");

To get in indian format we should use
e.g.
const dateByLocalization = new Date().toLocaleDateString("en-IN");

How to find time elapsed using the JavaScript Date Object ?
difference between two date can be obtained by substracting one from
other,we get diference in milliseconds.

const startDate = new Date('2023-08-15T00:00:00');
const endDate = Date.now(); // Current date and time in milliseconds

// Calculate the time difference in milliseconds
const timeDifferenceMS = endDate - startDate;
console.log("Time Difference in MilliSeconds",timeDifferenceMS)

Date Formatting:
ECMAScript provide Intl.DateTimeFormat to format date.
e.g.
const date001 = new Date(Date.UTC(2023, 9, 17, 13, 1, 0));
console.log(new Intl.DateTimeFormat().format(date001));

Output:
17/10/2023
Here we are passing month as 9 but getting 10 as months is zero
based index in javascript.

To Intl.DateTimeFormat we can pass parameter which will tell which
local to be used.
e.g.
const date002 = new Date(Date.UTC(2013, 09, 17, 13, 10, 20));
console.log("In en-US: ", new Intl.DateTimeFormat("en-US")
.format(date002));

It is possible to pass options as parameter to function
Intl.DateTimeFormat
e.g.
// request a weekday along with a long date
let options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
console.log(new Intl.DateTimeFormat("en-US", options)
.format(date003));
Output:
Tuesday, October 17, 2023

Third Party Date Library:

date-fns: date-fns is a modern JavaScript date utility library that
provides a wide range of functions for formatting, parsing,
manipulating, and comparing dates and times. It emphasizes simplicity
and immutability.

Moment.js was one of the most popular date libraries in the JavaScript
ecosystem. It provides a comprehensive set of date manipulation and
formatting functions.However, as of September 2020, it has been
considered legacy.

No comments:

Post a Comment