JavaScript Get-Date Methods

The JavaScript Get-Date Methods are used to get information from a date object.

In this post, we’ll learn how to retrieve different date methods in Javascript from the date object. In JavaScript, there are several ways to get the date. A date object’s data values can range from years, months, days, hours, minutes, seconds, and milliseconds.

The list of date methods to retrieve various dates from the Date object in Javascript is shown below.

getDate(): It is used to get the day as a number (1-31).

getTime(): It used to return the number of milliseconds since 1 January 1970.

date.now(): It is used to return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

getDay(): It is used to get the weekday as a number (0-6).

getSeconds(): It is used to get the seconds (0-59).

getMinutes(): It is used to get the minutes (0-59).

getMilliseconds(): It is used to get the milliseconds (0-999).

getHours(): It is used to get the hour (0-23).

getMonth(): It is used to get the month (0-11).

getFullYear(): It is used to get the year.


  • The getTime() Method: The getTime() a method is used to get the number of milliseconds since January 1, 1970:
<!DOCTYPE html>
<html>
<body>

<script>
const t = new Date();
document.write(t.getTime());
</script>
</body>
</html> 

Output:

image 91
  • The getFullYear() Method: The getFullYear() the method is used to get the year of a date as a 4-digit number:
<!DOCTYPE html>
<html>
<body>

<script>
const y = new Date();
document.write(y.getFullYear());
</script>
</body>
</html> 

Output:

image 92
  • The getMonth() Method: The getMonth() method is used to get the month of a date as a number (0-11):
<!DOCTYPE html>
<html>
<body>

<script>
const m = new Date();
document.write(m.getMonth());
</script>

</body>
</html> 
image 93

We can use an array of month names:

<!DOCTYPE html>
<html>
<body>

<script>
const m = new Date();
const mon = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
document.write(mon[m.getMonth()]);
</script>
</body>
</html> 
image 94
  • The getDay() Method: The getDay() method is used to get the weekday of a date as a number (0-6):
<!DOCTYPE html>
<html>
<body>

<script>
const dy = new Date();
document.write(dy.getDay());
</script>
</body>
</html>

Output:

image 125

We can use an array of day names:

<!DOCTYPE html>
<html>
<body>

<script>
const t = new Date();
const ds = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
document.write(ds[t.getDay()]);
</script>
</body>
</html> 

Output:

image 126
  • The getDate() Method: The getDate() the method is used to get the day of a date as a number (1-31):
<!DOCTYPE html>
<html>
<body>

<script>
const t = new Date();
document.write(t.getDate());
</script>

</body>
</html> 
image 95
  • The getHours() Method: The getHours() the method is used to return the hours of a date as a number (0-23):
<!DOCTYPE html>
<html>
<body>

<script>
const h = new Date();
document.write(h.getHours());
</script>

</body>
</html>

Output:

image 121
  • The getMinutes() Method: The getMinutes() the method is used to get the minutes of a date as a number (0-59):
<!DOCTYPE html>
<html>
<body>

<script>
const m = new Date();
document.write(m.getMinutes());
</script>

</body>
</html>

Output:

image 122
  • The getSeconds() Method: The getSeconds() the method is used to get the seconds of a date as a number (0-59):
<!DOCTYPE html>
<html>
<body>

<script>
const s = new Date();
document.write(s.getSeconds());
</script>

</body>
</html>

Output:

image 123
  • The getMilliseconds() Method: The getMilliseconds() method is used to get the milliseconds of a date as a number (0-999):
<!DOCTYPE html>
<html>
<body>

<script>
const ms = new Date();
document.write(ms.getMilliseconds());
</script>

</body>
</html>

Output:

image 124

UTC JavaScript Get-Date Methods

For working with UTC dates (Universal Time Zone dates), UTC date methods are used.

MethodExplanation
getUTCDate()It is used to get the UTC date
getUTCDay()It is used to get the UTC day
getUTCFullYear()It is used to get returns the UTC year
getUTCHours()It is used to get the UTC hour
getUTCMilliseconds()It is used to get the UTC milliseconds
getUTCMinutes()It is used to get the UTC minutes
getUTCMonth()It is used to get the UTC month
getUTCSeconds()It is used to get the UTC seconds