Explain the Date() object in JavaScript?



In JavaScript, Date object is an in-built object that stores date and time. It also provides a number of built-in methods in JavaScript like getDate() and getTime() etc. They are used to access the current date and time respectively.

var d = new Date();

Also, we can write the date and time format inside the date object.

var d = new Date(year,month,day,hour,minute,second);

Through Date object we can get current location, date, time in proper format.

Example 1

In the above program, as you can see that we simply created a date object and through the date object we can find the current date and time of the particular location.

<!DOCTYPE html> <html> <head> <title>Date Object</title> </head> <body> <script> var dt = new Date(); document.write(dt); </script> </body> </html>

Using getFullYear() method

In JavaScript getFullYear() is also an in-built method. Through getFullYear() method, we can fetch the current year. Following is the syntax of this method −

var dt = new Date();
document.write(dt.getFullYear());

Example

Let’s discuss with a suitable example. In JavaScript, we have lots of date and time format built-in methods; however, we used the getFullYear() method here to output the current year.

<!DOCTYPE html> <html> <head> <title>Date Object</title> </head> <body> <script> var dt = new Date(); var year = dt.getFullYear(); document.write("Year = "+ year); </script> </body> </html>

Using getDate()

By using the getDate() method we can find the current date. This is a built-in method in JavaScript. Following is the syntax of this method −

d.getDate();

Where d is the instance variable of the Date() object.

Let us try to understand with an example.

Example 1

In the given example, we have used the date and time format built-in method getDate() to find the current date.

<!DOCTYPE html> <html> <head> <title>Date Object</title> </head> <body> <script> var d = new Date(); var date = d.getDate(); document.write("Today's date is = " + date); </script> </body> </html>

Example 2

In this example, we are going to find the date and time using the date() object. The date object contains three elements in it; one to find the current year, next to find the current month and other to the find the current date.

<!DOCTYPE html> <html> <head> <title>Date Object</title> </head> <body> <script> var dt = new Date(); var year = dt.getFullYear(); var month = dt.getMonth(); var date = dt.getDate(); document.write("Today's date is : " + date + "/" + month + "/" + year); var hour = dt.getHours(); var minute = dt.getMinutes(); var second = dt.getSeconds(); document.write("<br>The current time is :" + hour + " : " + minute + " : " + second); </script> </body> </html>

In the above program, you can see that we have used all built-in time and date formatting methods to get the current date and current time.so it’s easy to understand. And also you can see the output of that program.

Dynamic Time using Date () object

To obtain a real-time date that changes dynamically, like how it shows on clocks on different smart devices, we will see how to use the Date() object.

Example

In the program below, we have used the setInterval (), getHour(), getMinute(), and getSecond() method to get the current time in the dynamic format; which means that the time will update alongside the real-time clock with respect to seconds.

setInterval()

The setInterval() method contains two parameters; one takes the function and the other is the time.

setInterval(function name, time);

We can see this method in the above example −

Example

<!DOCTYPE html> <html> <head> <title>Date Object</title> </head> <span id = 'demo'></span> <body> <script> setInterval(index, 1000); function index(){ var d = new Date(); var hour = d.getHours(); var minute = d.getMinutes(); var second = d.getSeconds(); document.getElementById('demo').innerHTML = "Time is : " + hour + " : " + minute + " : " + second; } </script> </body> </html>

Advertisements