Date Object

The date object displays the current date and time. A Date object is created using the keyword new.

Syntax

dateObjectname= new Date();



<script>
var todaysDate=new Date();
document.write(todaysDate);
</script>


Displays




Method

The Date() object has several methods.


toLocalString()

The toLocalString() method converts the object into string.

<script>
d=new Date();
document.write("Converted into string:" + d.toLocaleString());
document.write("<br>" + "<br>");
document.write(d);
</script>



Displays




getDate()

The getDate() method returns the day of the month according to the local time. The return value starts from 1 to 31.


<script>
var todaysDate=new Date();
document.write(todaysDate.getDate());
</script>

Displays




getDay()

The getDay() method returns the day of the week according to the local time. The return value starts from 0 to 6.

<script>
var todaysDay=new Date();
document.write(todaysDay.getDay());
</script>

Displays




getFullYear()

The getDay() method returns the year according to the local time.

<script>
var year=new Date();
document.write(year.getFullYear());
</script>

Displays




In the table below you can see some more of the date methods according to the local time.

To help you understand the table we have declared a new date object called d.

   Methods Explanation Example
getHours() returns the hour - values 0-23 d.getHours()
getMinutes() returns the minutes - values:0-59 d.getMinutes()
getSeconds() returns the seconds - values:0-59 d.getSeconds()
getMilliseconds() returns the milliseconds - values:0-999 d.getMilliseconds()
getMonth() returns the month - values:0-11. d.getMonth()
getTime() returns the number of milliseconds since 1/1/1970 for the date object d.getTime()
setDate() sets the day of the month - values:1-31 d.setDate(2)
setFullYear() sets the full year d.setFullYear(2021)
setHours() sets the hour - values:0-23 d.setHours(5)
setMinutes() sets the minutes - values:0-59 d.setMinutes(30)
setSeconds() sets the seconds - values:0-59 d.setSeconds(3)
setMilliseconds() sets the milliseconds - values:0-999 d.setMilliseconds(100)
setMonth() returns the month - values: 0-11 d.setMonth(6)
setTime() sets the number of milliseconds since 1/1/1970 d.setTime()



The table below shows more of the date methods according to the Coordinated Universal Time.


Methods Explanation Example
getUTCDate() returns the day of the month - values:1-31 d.getUTCDate()
getUTCDay() returns the day of the week - values:0-6 d.getUTCDate()
getUTCMonth() returns the month - values:0-11 d.getUTCMonth()
getUTCFullYear() returns the full year d.getUTCFullYear()
getUTCHours() returns the hour - values 0-23 d.getUTCHours()
getUTCMinutes() returns the minutes - values:0-59 d.getUTCMinutes()
getUTCSeconds() returns the seconds - values:0-59 d.getUTCSeconds()
getUTCMilliseconds() returns the milliseconds - values:0-999 d.getUTCMilliseconds()
setUTCDate() sets the day of the month - values:1-31 d.setUTCDate(2)
setUTCMonth() returns the month - values starts from 0-11. d.setUTCMonth(6)
setUTCFullYear() sets the full year d.setUTCFullYear(2021)
setUTCHours() sets the hour - values 0-23 d.setUTCHours(5)
setUTCMinutes() sets the minutes - values:0-59 d.setUTCMinutes(30)
setUTCSeconds() sets the seconds - values:0-59 d.setUTCSeconds(3)
setUTCMilliseconds() sets the milliseconds - values:0-999 d.setUTCMilliseconds(100)



Example 1

<script>
var d=new Date();
//day value starts from 0 to 6 - sunday=0
var days=new Array("Sunday","Monday",
"Tuesday","Wednesday","Thursday","Friday",
"Saturday")

document.write("Today is" + d.getDay());
document.write("today is" + days[getDay()];
</script>

Displays





Example 2

<script>
var d=new Date();
var months=new Array("January","February","March",
"April","May","June","July","August","September",
"October","November","December")

//The month value starts from 0 to 11
document.write("Month is" + d.getMonth());
document.write("Month is" + months[d.getMonth()];
</script>

Displays






Example 3

<script>
var today=new Date();
var d=new Date("August 22, 2018");

document.write(today);
document.write("Date is:d);


//change the month to April
d.setMonth(3);
document.write("Month changed:"+ d);

//change the year to 2020
d.setFullYear(2020);
document.write ("Year Changed"+ d);
</script>



Displays





Example 4

<script>
var d=new Date( );
document.write("Hours:" + d.getHours());
document.write("Minutes:" + d.getMinutes());
document.write("Time:" );
document.write(d.getHours()+ ":"+ d.getMinutes());
</script>

Displays




Now do the Exercises!