JavaScript Date Functions

In this post, we will discuss different date functions to get the details about date.

1. getDate()

The getDate() method return the Date from the TimeStamp. It won't take any parameters.

Syntax:

date.getDate()

Example:-

Let's return the Date.

CopiedCopy Code
<html>
<body>
<script>
	const date = new Date("May 25, 2022 11:45:20");
        // date
	document.writeln(date+"<br>");
    //  Return date
    document.writeln("Date: "+date.getDate());
    </script>	
</body>
</html>

Output:

Wed May 25 2022 11:45:20 GMT-0400 (Eastern Daylight Time)
Date: 25

The date is 25.

2. getDay()

The getDay() method return the day from the TimeStamp. It won't take any parameters. The day will be returned in the form of an integers.

  1. Sunday=0
  2. Monday=1
  3. Tuesday=2
  4. Wednesday=3
  5. Thursday=4
  6. Friday=5
  7. Saturday=6

Syntax:

date.getDay()

Example:-

Let's return the Day.

CopiedCopy Code
<html>
<body>
<script>
	const date = new Date("May 25, 2022 11:45:20");
        // date
	document.writeln(date+"<br>");
    //  Return day
    document.writeln("Day: "+date.getDay());
    </script>	
</body>
</html>

Output:

Wed May 25 2022 11:45:20 GMT-0400 (Eastern Daylight Time)
Day: 3

3 is returned as the day is Wednesday.

3. getFullYear()

The getFullYear() method return the year in four-digit format from the TimeStamp. It will not take any parameters.

Syntax:

date.getFullYear()

Example:-

Let's return the Year.

CopiedCopy Code
<html>
<body>
<script>
	const date = new Date("May 25, 2022 11:45:20");
        // date
	document.writeln(date+"<br>");
    //  Return year
    document.writeln("Year: "+date.getFullYear());
    </script>	
</body>
</html>

Output:

Wed May 25 2022 11:45:20 GMT-0400 (Eastern Daylight Time)
Year: 2022
So, the Year is 2022.

4. getMonth()

The getMonth() method return the month in the integer format from the TimeStamp. It will not take any parameters.

  1. January=0
  2. February=1
  3. ...
  4. December=11

Syntax:

date.getMonth()

Example:-

Let's return the Month.

CopiedCopy Code
<html>
<body>
<script>
	const date = new Date("May 25, 2022 11:45:20");
        // date
	document.writeln(date+"<br>");
    //  Return month
    document.writeln("Month: "+date.getMonth());
    </script>	
</body>
</html>

Output:

Wed May 25 2022 11:45:20 GMT-0400 (Eastern Daylight Time)
Month: 4

So, the Month is May since 4 is returned.

5. getHours()

The getHours() method return Hours from the TimeStamp. It will not take any parameters.

Syntax:

date.getHours()

Example:-

Let's return Hours.

CopiedCopy Code
<html>
<body>
<script>
	const date = new Date("May 25, 2022 11:45:20");
        // date
	document.writeln(date+"<br>");
    //  Return hours
    document.writeln("Hours: "+date.getHours());
    </script>	
</body>
</html>

Output:

Wed May 25 2022 11:45:20 GMT-0400 (Eastern Daylight Time)
Hours: 11

So, the Hours is 11.

6. getMinutes()

The getMinutes() method return Minutes from the TimeStamp. It will not take any parameters. Syntax:

date.getMinutes()

Example:-

Let's return Minutes.

CopiedCopy Code
<html>
<body>
<script>
	const date = new Date("May 25, 2022 11:45:20");
        // date
	document.writeln(date+"<br>");
    //  Return minutes
    document.writeln("Minutes: "+date.getMinutes());
    </script>	
</body>
</html>

Output:

Wed May 25 2022 11:45:20 GMT-0400 (Eastern Daylight Time)
Minutes: 45

So, the Minutes is 45.

7. getSeconds()

The getSeconds() method return Seconds from the TimeStamp. It will not take any parameters.

Syntax:

date.getSeconds()

Example:-

Let's return Seconds.

CopiedCopy Code
<html>
<body>
<script>
	const date = new Date("May 25, 2022 11:45:20");
        // date
	document.writeln(date+"<br>");
    //  Return seconds
    document.writeln("Seconds: "+date.getSeconds());
    </script>	
</body>
</html>

Output:

Wed May 25 2022 11:45:20 GMT-0400 (Eastern Daylight Time)
Seconds: 20

So, the Seconds is 20.