JavaScript getMonth 函数

JavaScript getMonth 函数是 Date 函数之一,它返回给定日期的月份编号。此 getMonth 函数返回的月份编号从 0(一月)开始,到 11(十二月)结束。

我们使用 get Month 来返回当前日期和时间中的月份编号(从 0 开始,到 11 结束)。

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptGetMonthFunction </title>
</head>
<body>
    <h1> JavaScriptgetMonthFunctionExample </h1>
<script>
  var dt = Date();  
  document.write("Date and Time : " + dt);
  document.write("Month Number using getMonth(): " + dt.getMonth());
</script>
</body>
</html>
getMonth Function Example

JavaScript get Month 示例返回自定义日期的月份编号。

<!DOCTYPE html>
<html>
<head>
    <title> JS </title>
</head>
<body>
    <h1> Example </h1>
<script>
  var dt = Date("January 12, 2016 10:09:34");
  document.write("Date and Time : " + dt);
  document.write("Month Number : " + dt.getMonth());
</script>
</body>
</html>
Example

Date and Time: Tue Jan 12 2016 10:09:34 GMT+0530 (Indian Standard Time)
Month Number : 0

在此 getMonth 函数示例中,我们从自定义年份(我的意思是,没有月份或日期)中提取月份。

<!DOCTYPE html>
<html>
<head>
    <title> JS </title>
</head>
<body>
    <h1> Example </h1>
<script>
  var dt = Date("2016 10:09:34");
  document.write("DateTime : " + dt);
  document.write("Month Number : " + dt.getMonth());
</script>
</body>
</html>
Example

DateTime: Fri Jan 01 2016 10:09:34 GMT+0530 (Indian Standard Time)
Month Number : 0