JavaScript getDay 函数

JavaScript getDay 函数是 Date 函数之一,用于从给定日期返回星期几。此 getDay 返回的值为:0 表示星期日,1 表示星期一,2 表示星期二,以此类推。

下面的示例将帮助您理解 getDay 函数。在这里,我们使用 getDay 从当前日期和时间返回星期几(星期几的数字)。

<!DOCTYPE html>
<html>
<head>
    <title> JS </title>
</head>
<body>
    <h1> Example </h1>
<script>
  var dt = Date();  
  document.write("Date and Time :  " + dt);
  document.write("Day : " + dt.getDay());
</script>
</body>
</html>
Example

Date and Time: Sat Nov 03 2018 16:34:32 GMT+0530 (Indian Standard Time)
Day : 6

在此 JavaScript get Day 函数示例中,我们正在从自定义日期提取星期几。

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptDateFunctions </title>
</head>
<body>
    <h1> JavaScriptgetDayFunctionExample </h1>
<script>
  var dt = Date("January 22, 2014 10:09:07");
  document.write("Date and Time : " + dt);
  document.write("Day from getDay(): " + dt.getDay());
</script>
</body>
</html>
getDay Function Example