JavaScript Date Methods

There are many methods available in JavaScript date object to get and set various date values i.e. year, month, day, minute, hour, second and millisecond.

Get Methods

Below methods are used to get different part of date

getDate()

getDate() methods returns day part of date between 1 to 31

<!DOCTYPE html>
<html>
<body>

Current Date Object: <br /><span id="dateobj"></span><br /><br />
getDate() returns date part of Date object.<br />
Current Date : <span id="date"></span><br /><br />
getDay() returns weekday from Date object.<br />
Current Weekday : <span id="weekday"></span><br /><br />
getFullYear() returns 4 digit year from Date object.<br />
Current Year : <span id="year"></span><br /><br />
getHours() returns 24hour format hour value.<br />
Current Hour : <span id="hour"></span><br /><br />

getMilliseconds() returns milliseconds.<br />
Milliseconds : <span id="msecond"></span><br /><br />

getMinutes() returns minutes.<br />
Current Minutes : <span id="minutes"></span><br /><br />
getMonth() returns month value from date object.<br />
Current Month : <span id="month"></span><br /><br />
getSeconds() returns 24hour format hour value.<br />
Current second : <span id="second"></span><br /><br />
getTime() returns 24hour format hour value.<br />
Current milliseconds passed since 01/01/1970: <span id="time"></span><br /><br />

<script>
var date = new Date();

document.getElementById('dateobj').innerHTML=date;
document.getElementById('date').innerHTML=date.getDate();
document.getElementById('weekday').innerHTML=date.getDay();
document.getElementById('year').innerHTML=date.getFullYear();
document.getElementById('hour').innerHTML=date.getHours();
document.getElementById('msecond').innerHTML=date.getMilliseconds();
document.getElementById('minutes').innerHTML=date.getMinutes();
document.getElementById('month').innerHTML=date.getMonth();
document.getElementById('second').innerHTML=date.getSeconds();
document.getElementById('time').innerHTML=date.getTime();

</script>

</body>
</html>

Download the code Run the code

getDay()

getDay() methods returns weekday part of date between 0 to 6. 0 denotes first day of week as Sunday.

getFullYear()

getFullYear() methods returns four digit year part of date in YYYY format.

var date = new Date();
var year = date.getFullYear();

getHours()

getHours() returns two digit hour part of date in 24 hour format.

var date = new Date();
var hour= date.getHours();

getMilliseconds()

getMilliseconds() returns millisecond part of date.

var date = new Date();
var mseconds = date.getMilliseconds();

getMinutes()

getMinutes() returns minute part of date.

var date = new Date();
var minutes = date.getMinutes();

getMonth()

getMonth() returns month part of date between 0 to 11. 0 for January and 11 for December

var date = new Date();
var month= date.getMonth();

getSeconds()

getSeconds() returns seconds part of date.

var date = new Date();
var second = date.getSeconds();

getTime()

getTime() returns milliseconds since 1st January 1970.

var date = new Date();
var time= date.getTime();

Set Methods

Date object set methods are used set various date parameters.

setDate()

setDate() methods sets date between 1 to 31. setDate() is also used to add days to date. Adding days will automatically change month/year based on added days if required.

<!DOCTYPE html>
<html>
<body>

Current Date Object: <br /><span id="dateobj"></span><br /><br />
setDate() sets date of Date object.<br />
New Date : <span id="date"></span><br /><br />
New Weekday : <span id="weekday"></span><br /><br />
setFullYear() sets 4 digit year for Date object.<br />
New Year : <span id="year"></span><br /><br />
setHours() set hours for date object.<br />
New Hour : <span id="hour"></span><br /><br />

setMilliseconds() sets milliseconds.<br />
Milliseconds : <span id="msecond"></span><br /><br />

setMinutes() sets minutes.<br />
New Minutes : <span id="minutes"></span><br /><br />
setMonth() sets month value for date object.<br />
New Month : <span id="month"></span><br /><br />
setSeconds() sets seconds for date object.<br />
New second : <span id="second"></span><br /><br />

<script>
var date = new Date();
document.getElementById('dateobj').innerHTML=date;

date.setDate(30);
document.getElementById('date').innerHTML=date;
document.getElementById('weekday').innerHTML=date.getDay();
date.setFullYear(2015)
document.getElementById('year').innerHTML=date.getFullYear();
date.setHours(21);
document.getElementById('hour').innerHTML=date.getHours();
date.setMilliseconds(900);
document.getElementById('msecond').innerHTML=date.getMilliseconds();
date.setMinutes(50);
document.getElementById('minutes').innerHTML=date.getMinutes();
date.setMonth(11);
document.getElementById('month').innerHTML=date.getMonth();
date.setSeconds(59);
document.getElementById('second').innerHTML=date.getSeconds();

</script>

</body>
</html>

Download the code Run the code

setFullYear()

setFullYear() methods sets year for current date object.

var date = new Date();
var year = date.setFullYear();

setHours()

setHours() sets hours for date object.

var date = new Date();
var year = date.setHours();

setHours()

setHours() sets hours for date object.

var date = new Date();
var year = date.setHours();

setMilliseconds()

setMilliseconds() sets milliseconds between 0 to 999 for date object.

var date = new Date();
var year = date.setMilliseconds();

setMinutes()

setMinutes() sets minutes between 0 to 59 for date object.

var date = new Date();
var year = date.setMinutes();

setMonth()

setMonth() sets month between 0 to 11 for date object.

var date = new Date();
var year = date.setMonth();

setSeconds()

setSeconds() sets seconds between 0 to 59 for date object.

var date = new Date();
var year = date.setSeconds();

Compare dates

With JavaScript date object two dates can be easily compared using getTime() method.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>
<script>
var date1 = new Date();
var date2 = new Date();
date2.setDate(date2.getDate() + 1);
var same = date1 === date2;

if(same) {
	document.getElementById('demo').innerHTML='Dates are equal';
} else {
	document.getElementById('demo').innerHTML='Dates are not equal';
}

</script>

</body>
</html>

Download the code Run the code

UTC date methods

UTC date methods are available to work with UTC timezone.

Method					Description
getUTCDate()			Same as getDate(), but returns the UTC date
getUTCDay()				Same as getDay(), but returns the UTC day
getUTCFullYear()		Same as getFullYear(), but returns the UTC year
getUTCHours()			Same as getHours(), but returns the UTC hour
getUTCMilliseconds()	Same as getMilliseconds(), but returns the UTC milliseconds
getUTCMinutes()			Same as getMinutes(), but returns the UTC minutes
getUTCMonth()			Same as getMonth(), but returns the UTC month
getUTCSeconds()			Same as getSeconds(), but returns the UTC seconds

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .