JavaScript Get Date Methods
The new Date() Constructor
To make a date in JavaScript, you use new Date()
.
new Date()
provides you with a date object showing the current date and time.
Date Get Methods
Method |
Description |
getFullYear() |
Get year as a four digit number (yyyy) |
getMonth() |
Get month as a number (0-11) |
getDate() |
Get day as a number (1-31) |
getDay() |
Get weekday as a number (0-6) |
getHours() |
Get hour (0-23) |
getMinutes() |
Get minute (0-59) |
getSeconds() |
Get second (0-59) |
getMilliseconds() |
Get millisecond (0-999) |
getTime() |
Get time (milliseconds since January 1, 1970) |
Note 1
The get methods above return Local time.
The time called Universal Time (UTC) is written at the bottom of this page.
Note 2
The get methods give you details from date objects you already have.
In a date object, the time doesn't change. The clock is not running.
The time stored in a date object isn't the same as the current time.
The getFullYear() Method
The getFullYear()
function gives you the year of a date in a four-digit format.
Warning !
Some older JavaScript code might use a method called getYear() which is not standard.
The function getYear() should give back a year in a short, two-digit format.
getYear() is deprecated. Do not use it!
The getMonth() Method
The getMonth()
function gives you the month of a date, but it's represented as a number from 0 to 11.
Note
In JavaScript, January is represented by the number 0, February by 1, and so on.
December, on the other hand, is represented by the number 11.
Note
You can use an array of names to return the month as a name:
The getDate() Method
The getDate()
function gives you the day of a date as a number from 1 to 31.
The getHours() Method
The getHours()
function gives you the time in hours as a number, ranging from 0 to 23, from a given date.
The getMinutes() Method
The getMinutes()
function gives you the number of minutes in a date, ranging from 0 to 59.
The getSeconds() Method
The getSeconds()
function gives you the number of seconds in a date, ranging from 0 to 59.
The getMilliseconds() Method
The getMilliseconds()
function gives you the number of milliseconds in a date, ranging from 0 to 999.
The getDay() Method
The getDay()
function gives you the day of the week as a number (0-6) for a specific date.
Note
In JavaScript, Sunday is considered the first day of the week, marked as day 0.
However, in some countries, Monday is seen as the beginning of the week instead of Sunday.
Note
You can use an array of names, and getDay()
to return weekday as a name:
The getTime() Method
The getTime()
function gives you the count of milliseconds that have passed since January 1, 1970.
The Date.now() Method
The Date.now()
function gives you the count of milliseconds that have passed since January 1, 1970.
The Date.now()
function is a feature of the Date object and returns the current timestamp.
You can't apply it to a date object, for example, like this: myDate.now()
.
The syntax is always Date.now()
.
UTC Date Get Methods
Method |
Same As |
Description |
getUTCDate() |
getDate() | Returns the UTC date |
getUTCFullYear() |
getFullYear() | Returns the UTC year |
getUTCMonth() |
getMonth() | Returns the UTC month |
getUTCDay() |
getDay() | Returns the UTC day |
getUTCHours() |
getHours() | Returns the UTC hour |
getUTCMinutes() |
getMinutes() | Returns the UTC minutes |
getUTCSeconds() |
getSeconds() | Returns the UTC seconds |
getUTCMilliseconds() |
getMilliseconds() | Returns the UTC milliseconds |
The getTimezoneOffset() Method
The getTimezoneOffset()
function gives you the time difference, in minutes, between your local time and UTC time.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getFullYear() Method</h2>
<p>Return the full year of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25")
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMonth() Method</h2>
<p>Return the month of a date as a number from 0 to 11.</p>
<p>To get the correct month number, you must add 1:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>JavaScript getMonth()</h2>
<p>Return the month as a number.</p>
<p>You can utilize an array of names to retrieve the month as a name.</p>
<p id="demo"></p>
<script>
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDate() Method</h2>
<p>Return the day of a date as a number (1-31):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDate();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getHours() Method</h2>
<p>Return the hours of a date as a number (0-23):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getHours();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMinutes() Method</h2>
<p>Returns the minutes of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMinutes();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getSeconds() Method</h2>
<p>Return the seconds of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getSeconds();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMilliseconds() Method</h2>
<p>Return the milliseconds of a date as a number (0-999):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDay();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number.</p>
<p>You can use a array of names to get the name of the day:</p>
<p id="demo"></p>
<script>
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const d = new Date("2021-03-25");
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>
<p id="demo"></p>
<script>
const d = new Date("1970-01-01");
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The Date.now() Method</h2>
<p>Return the current date/time in milliseconds since January 1, 1970:</p>
<p id="demo"></p>
<script>
const date = Date.now();
document.getElementById("demo").innerHTML = date;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTimezoneOffset() Method</h2>
<p>The time zone difference in minutes is:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTimezoneOffset();
</script>
</body>
</html>