Hey guys, welcome back, in this module, I will discuss date in JavaScript. In the previous modules, we learned objects in JavaScript, so if you don’t know what objects in JavaScript are, you can see our previous module. So, let’s begin.
Date in JavaScript
In JavaScript, a date object is used to get the date (year, month, day) and time (hours, minutes, seconds, milliseconds). Using date objects in JavaScript, you can display the timer and date on your web pages. By default, JavaScript can show a date as a full-text string using the client’s browser time zone.
How to create a date object in JavaScript?
JavaScript date objects can be created using the Date() constructor in the following ways:
- new Date()
Using this method, we will get the client’s machine’s current date and time.
Syntax
var variable_name=new Date();let obj=new Date(); console.log(obj);
- new Date(millisec)
let obj=new Date(2000); console.log(obj);
In the above example, 2000 milliseconds are equal to 2 seconds, since 1 second= 1000 milliseconds.
- new Date(string)
In this method, a date in the form of a string is passed as a parameter.let d = new Date("July 06 2021 11:13:00"); console.log(d);
- new Date(year, month, date, hour, min, sec, millisecond)
This method is used for creating date objects with specified date-time. In JavaScript, the numbering of months starts from 0 to 11. Thus, January is 0, February is 1, ….., December is 11.let e=new Date(2020, 4, 23, 15, 45, 30, 1000); console.log(e); let d=new Date(2019, 3, 2, 10, 5, 10); console.log(d); Specifying only year, month and day. const date=new Date(2021,5,4); console.log(date); Specifying only year and month const date=new Date(2021,5); console.log(date);
Date Methods in JavaScript
JavaScript date object provides a set of methods for getting and setting the values of year, month, day, hours, minutes, seconds, and milliseconds. Let’s discuss them in detail.
- getDate()
It is a method used to return an integer value between 1 to 31 based on the current date or specified date.
Syntax
Date_object.getDate()let date=new Date(); console.log(date.getDate()); //Output //22
let date=new Date(2021,6,10); console.log(date.getDate()); //Output //10
- getFullYear()
It is used to return the current year or the year you have specified.
Syntax
Date_object.getFullyear()var date=new Date(2019,6,10); console.log(date.getFullYear()); var d=new Date(); console.log(d.getFullYear()); //Output //2019 //2021
- getMonth()
This method is used to return an integer value between 0 to 11 based on the current date or given date.
Syntax
Date_object.getMonth()var date=new Date(2020,6,8); console.log(date.getMonth()); var d=new Date(); console.log(d.getMonth()); console.log(d); //Output //6 //4 //Sat May 22 2021 18:39:25 GMT+0530 (India Standard Time)
- getDay()
It is used to get the day of the week and returns an integer value between 0 and 6. Thus, Sunday is 0, Monday is 1, ….., Saturday is 6.
Syntax
Date_object.getDate()
var date=new Date();
console.log(date.toDateString())
console.log(date.getDay());var d=new Date(2020,10,4); console.log(d.toString()); console.log(d.getDay()); var obj=new Date("April 20 1999"); console.log(obj.toDateString()); console.log(obj.getDay()); //Output //Sat May 22 2021 //6 //Wed Nov 04 2020 00:00:00 GMT+0530 (India Standard Time) //3 //Tue Apr 20 1999 //2
- getHours()
It is used to return an integer value between 0 and 23 based on the local time or specified time.
Syntax
Date_object.getHours()var date=new Date(); console.log(date.toDateString()) console.log(date.getHours()); var d=new Date(2020,8,11,14); console.log(d.toDateString()); console.log(d.getHours()); //Output //Sat May 22 2021 //19 //Fri Sep 11 2020 //14
- getMinutes()
A date method is used to return an integer value between 0 and 59 based on the local time or specified time.
Syntax
Date_object.getMinutes()var date=new Date(); console.log(date.toDateString()) console.log(date.getMinutes()); var d=new Date(2021,9,11,10,45); console.log(d.toDateString()); console.log(d.getMinutes()); //Output //Sat May 22 2021 // 6 //Mon Oct 11 2021 //45
- getSeconds()
This date method is used to return an integer value between 0 and 60 based on the local time or specified time.
Syntax
Date_object.getSeconds()var date=new Date(); console.log(date.toDateString()) console.log(date.getSeconds()); var d=new Date(2021,9,11,10,45,32); console.log(d.toDateString()); console.log(d.getSeconds()); //Output //Sat May 22 2021 //50 //Mon Oct 11 2021 //32
- setDate()
This method is used to set the date to a specified value between 1 and 31.
Syntax
Date_object.setDate(value)var date= new Date(); console.log(date.getDate()); date.setDate(20); console.log(date.getDate()) //Output //22 //20
- setMonth()
This method is used to set the month to a specified value between 0 and 11.
Syntax
Date_object.setMonth(value)var date= new Date(); console.log(date.getMonth()); date.setMonth(1); console.log(date.getMonth()); //Output //4 //1
- setFullYear()
It is used to set the year to a specified value.
Syntax
Date_object.setFullYear(value)var date= new Date(); console.log(date.getFullYear()); date.setFullYear(1999); console.log(date.getFullYear()); //Output //2021 //1999
- setHours()
This method sets the hour value to a specified value.
Syntax
Date_object.setHours(value)var date= new Date(); console.log(date.getHours()); date.setHours(16); console.log(date.getHours()); //Output //19 //16
- setMinutes()
This date method sets the value of a minute to a specified value.
Syntax
Date_object.setMinutes(value)var date= new Date(); console.log(date.getMinutes()); date.setMinutes(49); console.log(date.getMinutes()); //Output //40 //49
There are many other date methods in JavaScript to get and set the date and time based on universal time.
I hope Date in JavaScript module has helped you a lot in gaining information about the date object in JavaScript. It will be clearer when you will implement it by yourself. Keep in touch with us for more modules like this. Keep coding!