JavaScript Tutorial/Date/getDay

Материал из Web эксперт
Перейти к: навигация, поиск

Date.getDay() with switch statement

<html>
    <script language="JavaScript">
    <!--
    function getDayString(num)
    {
      var day;    //Create a local variable to hold the string
      switch(num)
      {
        case 0:
          day="Sunday";
          break;
        case 1:
          day="Monday";
          break;
        case 2:
          day="Tuesday";
          break;
        case 3:
          day="Wednesday";
          break;
        case 4:
          day="Thursday";
          break;
        case 5:
          day="Friday";
          break;
        case 6:
          day="Saturday";
          break;
        default:
          day="Invalid day";
      }
      return day;
    }
    theDate = new Date();
    document.write("Today is ",getDayString(theDate.getDay()));
    -->
    </script>
    </html>


Get day from a date

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
dateVar = new Date();
alert(dateVar.getDay());
//  -->
</script>
</head>
<body>
</body>
</html>


Translate the Date().getDay() to a string day name

The getDay() method returns the day of the week expressed as an integer from 0 (Sunday) to 6 (Saturday).

The following example uses the getDate() Method to Return the Day of the Week



<html>
<head>
<title>Convert Day of the Week</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var weekdays = new Array(7);
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
var current_date = new Date();
weekday_value = current_date.getDay();
document.write("Today is " + weekdays[weekday_value]);
//-->
</script>
</body>
</html>