Home >>Javascript Tutorial >JavaScript Date

JavaScript Date

Date Object in JavaScript

The Date Object in JavaScript is used to get the day, month, and year. A timer can be displayed on the webpage with the help of JavaScript date object.

Different Date constructors can be used to create the date object. This object delivers the methods to get and set day, month, year, hour, minute, and seconds.

Constructor

There are 4 variants of date constructor available to create date object. The variants are as follows:

  • Date()
  • Date(milliseconds)
  • Date(dateString)
  • Date(year, month, day, hours, minutes, seconds, milliseconds)

Date Methods In JavaScript

Following is the list of Date Methods in JavaScript along a brief description:

Methods Description
getDate() This method returns the integer value between 1 and 31 representing the day for the specified date based on the local time.
getDay() This method returns the integer value between 0 and 6 representing the day of the week based on local time.
getFullYears() This method returns the integer value representing the year based on local time.
getHours() This method returns the integer value between 0 and 23 representing the hours based on local time.
getMilliseconds() This method returns the integer value between 0 and 999 representing the milliseconds based on the local time.
getMinutes() This method returns the integer value between 0 and 59 representing the minutes based on the local time.
getMonth() This method returns the integer value between 0 and 11 representing the month based on the local time.
getSeconds() This method returns the integer value between 0 and 60 representing the seconds based on the local time.
getUTCDate() This method returns the integer value between 1 and 31 representing the day for the specified date based on the universal time.
getUTCDay() This date method returns the integer value between 0 and 6 representing the day of the week based on the universal time.
getUTCFullYears() This method returns the integer value represents the year based on the universal time.
getUTCHours() This method returns the integer value between 0 and 23 representing the hours based on the universal time.
getUTCMinutes() This method returns the integer value between 0 and 59 representing the minutes based on the universal time.
getUTCMonth() This method returns the integer value between 0 and 11 representing the month based on the universal time.
getUTCSeconds() This method returns the integer value between 0 and 60 representing the seconds based on universal time.
setDate() This method sets the day value for the specified date based on the local time.
setDay() This method sets the particular day of the week based on the local time.
setFullYears() This method sets the year value for the specified date based on the local time.
setHours() This method sets the hour value for the specified date based on local time.
setMilliseconds() This method sets the millisecond value for the specified date based on the local time.
setMinutes() This method sets the minute value for the specified date based on the local time.
setMonth() This method sets the month value for the specified date based on the local time.
setSeconds() This method sets the second value for the specified date based on the local time.
setUTCDate() This method sets the day value for the specified date based on the universal time.
setUTCDay() This method sets the particular day of the week based on the universal time.
setUTCFullYears() This method sets the year value for the specified date based on the universal time.
setUTCHours() This method sets the hour value for the specified date Based on the universal time.
setUTCMilliseconds() This method sets the millisecond value for the specified date based on the of universal time.
setUTCMinutes() This method sets the minute value for the specified date based on the universal time.
setUTCMonth() This method sets the month value for the specified date based on the universal time.
setUTCSeconds() This method sets the second value for the specified date based on the universal time.
toDateString() This method returns the portion date of a Date object.
toISOString() This method returns the date in the form ISO format string.
toJSON() This method returns a string that represents the Date object. This date method also serializes the Date object throughout JSON serialization.
toString() This method returns the date in the form of string.
toTimeString() This method returns the time portion of a Date object.
toUTCString() This method converts the specified date in the form of string using UTC time zone.
valueOf() This method returns the primitive value of a Date object.

Date Example in JavaScript

Here is an example to print date object. This date object prints both date and time.

Let's understand this date object with these examples:

Current Date and Time: <span id="txt"></span>  
<script>  
var today=new Date();  
document.getElementById('txt').innerHTML=today;  
</script> 
Output :Current Date and Time: Fri Nov 15 2019 17:59:08 GMT+0530 (India Standard Time)

Here is another example to print the date, month or year.

<script>  
var date=new Date();  
var day=date.getDate();  
var month=date.getMonth()+1;  
var year=date.getFullYear();  
document.write("<br>Date is: "+day+"/"+month+"/"+year);  
</script> 
Output :Date is: 15/11/2019

Current Time Example in JavaScript

Here is an example to print the current time of the system:

Current Time: <span id="txt"></span>  
<script>  
var today=new Date();  
var h=today.getHours();  
var m=today.getMinutes();  
var s=today.getSeconds();  
document.getElementById('txt').innerHTML=h+":"+m+":"+s;  
</script>  
Output :Current Time: 18:3:12

Digital Clock example in JavaScript

Here is the simple example of displaying the digital clock with the help of JavaScript Date Object. Please note that SetTimeout() and SetInterval () methods are the two ways to set the interval in the JavaScript.

Current Time: <span id="txt"></span>  
<script>  
window.onload=function(){getTime();}  
function getTime(){  
var today=new Date();  
var h=today.getHours();  
var m=today.getMinutes();  
var s=today.getSeconds();  
// add a zero in front of numbers<10  
m=checkTime(m);  
s=checkTime(s);  
document.getElementById('txt').innerHTML=h+":"+m+":"+s;  
setTimeout(function(){getTime()},1000);  
}  
//setInterval("getTime()",1000);//another way  
function checkTime(i){  
if (i<10){  
  i="0" + i;  
 }  
return i;  
}  
</script>    
Output :The output of the above mentioned example is the current time along with the hour, minutes and seconds.

No Sidebar ads