JavaScript Get Date Methods

By | August 24, 2022

These methods can be used for getting information from a date object:

MethodDescription
getFullYear()Get the year as a four digit number (yyyy)
getMonth()Get the month as a number (0-11)
getDate()Get the day as a number (1-31)
getHours()Get the hour (0-23)
getMinutes()Get the minute (0-59)
getSeconds()Get the second (0-59)
getMilliseconds()Get the millisecond (0-999)
getTime()Get the time (milliseconds since January 1, 1970)
getDay()Get the weekday as a number (0-6)
Date.now()Get the time. ECMAScript 5.

The getTime() Method

The getTime() method returns the number of milliseconds since January 1, 1970:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getTime()</h2>
<p>The internal clock in JavaScript counts from midnight January 1, 1970.</p>
<p>The getTime() function returns the number of milliseconds since then:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>

Result:

JavaScript getTime()

The internal clock in JavaScript counts from midnight January 1, 1970.

The getTime() function returns the number of milliseconds since then:

1661366893233

The getFullYear() Method

The getFullYear() method returns the year of a date as a four digit number:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getFullYear()</h2>
<p>The getFullYear() method returns the full year of a date:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
</body>
</html>

Result:

JavaScript getFullYear()

The getFullYear() method returns the full year of a date:

2022

The getMonth() Method

The getMonth() method returns the month of a date as a number (0-11):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getMonth()</h2>
<p>The getMonth() method returns the month of a date as a number from 0 to 11.</p>
<p>To get the correct month, you must add 1:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
</body>
</html>

Result:

JavaScript getMonth()

The getMonth() method returns the month of a date as a number from 0 to 11.

To get the correct month, you must add 1:

8

In JavaScript, the first month (January) is month number 0, so December returns month number 11.

You can use an array of names, and getMonth() to return the month as a name:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getMonth()</h2>
<p>The getMonth() method returns the month as a number.</p>
<p>You can use an array to display the name of the month:</p>
<p id="demo"></p>
<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d = new Date();
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>
</body>
</html>

Result:

JavaScript getMonth()

The getMonth() method returns the month as a number.

You can use an array to display the name of the month:

August

The getDate() Method

The getDate() method returns the day of a date as a number (1-31):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getDate()</h2>
<p>The getDate() method returns the day of a date as a number (1-31):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDate();
</script>
</body>
</html>

Result:

JavaScript getDate()

The getDate() method returns the day of a date as a number (1-31):

24

The getHours() Method

The getHours() method returns the hours of a date as a number (0-23):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getHours()</h2>
<p>The getHours() method returns the hours of a date as a number (0-23):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getHours();
</script>
</body>
</html>

Result:

JavaScript getHours()

The getHours() method returns the hours of a date as a number (0-23):

12

The getMinutes() Method

The getMinutes() method returns the minutes of a date as a number (0-59):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getMinutes()</h2>
<p>The getMinutes() method returns the minutes of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMinutes();
</script>
</body>
</html>

Result:

JavaScript getMinutes()

The getMinutes() method returns the minutes of a date as a number (0-59):

28

The getSeconds() Method

The getSeconds() method returns the seconds of a date as a number (0-59):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getSeconds()</h2>
<p>The getSeconds() method returns the seconds of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getSeconds();
</script>
</body>
</html>

Result:

JavaScript getSeconds()

The getSeconds() method returns the seconds of a date as a number (0-59):

8

The getMilliseconds() Method

The getMilliseconds() method returns the milliseconds of a date as a number (0-999):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getMilliseconds()</h2>
<p>The getMilliseconds() method returns the milliseconds of a date as a number (0-999):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>
</body>
</html>

Result:

JavaScript getMilliseconds()

The getMilliseconds() method returns the milliseconds of a date as a number (0-999):

600

The getDay() Method

The getDay() method returns the weekday of a date as a number (0-6):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getDay()</h2>
<p>The getDay() method returns the weekday as a number:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
</script>
</body>
</html>

Result:

JavaScript getDay()

The getDay() method returns the weekday as a number:

3

In JavaScript, the first day of the week (0) means “Sunday”, even if some countries in the world consider the first day of the week to be “Monday”

You can use an array of names, and getDay() to return the weekday as a name:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript getDay()</h2>
<p>The getDay() method returns the weekday as a number.</p>
<p>You can use an array to display the name of the weekday:</p>
<p id="demo"></p>
<script>
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const d = new Date();
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>
</body>
</html>

Result:

JavaScript getDay()

The getDay() method returns the weekday as a number.

You can use an array to display the name of the weekday:

Wednesday

UTC Date Methods

UTC date methods are used for working with UTC dates (Universal Time Zone dates):

MethodDescription
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

Leave a Reply

Your email address will not be published. Required fields are marked *