Latest posts

10/recent/ticker-posts

Date Object in JavaScript | Displaying Date and time in Javascript

 Date Object in Javascript

What is the purpose of using Date Object?

In Javascript, the date( ) object is a predefined object present in Javascript that is used to display the date and time.
Suppose we want to display the current date and time in the result, we can easily able to use the date( ) object in our code.



Let's see how to use the date( ) object in Javascript.

1) Displaying Current Date and Time :

const dt = new Date();


2) Displaying Specific Date and Time by Passing Parameters in Object :
const dt = new Date("2023-03-10");


Output of Javascript Date Object :

By default, the javascript date( ) object displays the current date and time of the Browser.

Ways to create date() object in javascript :

In Javascript, we are able to create a date object using 9 ways.
Basically, to create a date object in javascript we can use the new date( ) constructor.
1) new Date()
2) new Date(date string)
3) new Date(year,month)
4) new Date(year,month,day)
5) new Date(year,month,day,hours)
6) new Date(year,month,day,hours,minutes)
7) new Date(year,month,day,hours,minutes,seconds)
8) new Date(year,month,day,hours,minutes,seconds,ms)
9) new Date(milliseconds)


Here's a twist are comes in the millisecond's object.
Javascript store all the dates in the form of milliseconds, from 1 Jan 1970.
According to this phenomenon, the current time is : Milliseconds.

Conversion of the Date object's result to another format:

We can able to convert our result of the Date object into other formats like DateString, UTCString, ISOString, LocaleString, LocaleDateString, LocaleTimeString, etc.

1) toDateString( ) :

It converts the lengthy results into a suitable Date format.
const dt = new Date();
dt.toDateString();

2) toUTCString( ) :

This method converts a date into UTC date format.
const dt = new Date();
dt.toUTCString();


3) toISOString( ) :

This method converts a date result into an ISO date format.
const dt = new Date();
dt.toISOString();


4) toLocaleString( ) :

This method converts a date result into an ISO date format.
const dt = new Date();
dt.toLocaleString();


5) toLocaleDateString( ) :

This method converts a date result into an ISO date format.
const dt = new Date();
dt.toLocaleDateString();


6) toLocaleTimeString( ) :

This method converts a date result into an ISO date format.
const dt = new Date();
dt.toLocaleTimeString();


That's all for Now...!
In the next post, we are going to see the Get and Set methods of the Date object.

I hope this post will help you to learn Javascript more...

Thank You...!




Also Visit


Post a Comment

0 Comments