JavaScript Date getMonth() 方法详解与使用示例

作者:半夏时间:2026-10-29 10:51:43浏览:378

本文介绍JavaScript中Date对象的getMonth()方法,用于获取指定日期的月份。该方法返回0到11之间的整数,其中0代表一月,11代表十二月。文章提供了具体的语法说明及基于Date构造函数的代码示例,展示如何正确获取月份数值。

getMonth() 方法概述

JavaScript日期的getMonth()方法用于按照本地时间返回指定日期中的月份数值。该方法返回的是一个0到11之间的整数,其中0对应一月,1对应二月,依此类推。


Date.getMonth()

参数说明

以下是关于该方法参数的详细说明:

  • 无参数。

返回值说明

返回值:该方法按照本地时间返回指定日期的月份数值。

代码示例

示例演示:


<html>
<head>
<title>JavaScript getMonth Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date( "December 25, 1995 23:15:00" );
  document.write("getMonth() : " + dt.getMonth() ); 
</script>
</body>
</html>

执行上述代码将产生以下结果:


getMonth() : 11 

作者最新文章