Computer >> Computer tutorials >  >> Programming >> Javascript

Date.getDay() function in JavaScript


The Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The getDay() function of the Date object returns the number representing day of the week of its current date (0 represents Sunday and so on).

Syntax

Its syntax is as follows

dateObj.getDay();

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 1989 00:4:00');
      document.write(dateObj.getDay());
   </script>
</body>
</html>

Output

2

Example

If you do not mention the date of the month it is considered as the date is first and the day of the week is returned accordingly. For example we have mentioned the month as September 1989 and we haven’t mentioned the date so, by default it is considered that the date is 1 and since it is Thursday and since it is 5th day of the week this function returns 5.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('September, 1989 00:4:00');
      document.write(dateObj.getDay());
   </script>
</body>
</html>

Output

5

Example

If you do not pass any value to the constructor while creating the date object then this function returns the day of the week of the current date.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date();
      document.write(dateObj.getDate());
   </script>
</body>
</html>

Output

24