腾讯云618限量抢购

JavaScript Date getMonth 方法:取得表示月份的数字

最后更新:2012-05-21 17:15阅读:10161

JavaScript Date getMonth 方法

getMonth 方法用于取得 Date 对象中表示月份的数字。语法如下:

date_obj.getMonth()

提示

  • 该方法总是结合一个 Date 对象来使用。
  • 得到 0-11 的数字表示 1-12 月份。

getMonth 方法实例

例子 1

该例子从当前的时间中取得月份数字:

<script language="JavaScript">

var d = new Date();
document.write( d.getMonth() );

</script>

运行该例子,输出:

3

注意:实际的月份是 4 月。

例子 2

该例子正确的输出自定义的月份:

<script language="JavaScript">

var d = new Date();
month_array = new Array('一','二','三','四','五','六','七','八','九','十','十一','十二');
document.write( month_array[d.getMonth()] + '月' );

</script>

运行该例子,输出:

四月

本章节内容共分 18 部分:
  1. 1. JavaScript Date 对象(日期与时间)
  2. 2. JavaScript Date getFullYear 方法:取得 4 位数字的年份
  3. 3. JavaScript Date getMonth 方法:取得表示月份的数字
  4. 4. JavaScript Date getDate 方法:取得月份中的某天
  5. 5. JavaScript Date getDay 方法:取得一周的某天
  6. 6. JavaScript Date getHours 方法:取得时间中的小时
  7. 7. JavaScript Date getMinutes 方法:取得时间中的分钟
  8. 8. JavaScript Date getSeconds 方法:取得时间中的秒数
  9. 9. JavaScript Date getMilliseconds 方法:取得时间中的毫秒
  10. 10. JavaScript Date getTime 方法:取得时间戳(距1970年1月1日的毫秒数)
  11. 11. JavaScript Date getTimezoneOffset 方法:计算格林威治时间与本地时间的时差
  12. 12. JavaScript Date parse 方法:将时间字符串转换为时间戳(毫秒)
  13. 13. JavaScript Date 设置年月日:setFullYear、setMonth 与 setDate 方法
  14. 14. JavaScript Date 设置时分秒毫秒:setHours、setMinutes、setSeconds 与 setMilliseconds 方法
  15. 15. JavaScript Date toString 方法:将 Date 对象转换为字符串
  16. 16. JavaScript Date toDateString 与 toTimeString 方法
  17. 17. JavaScript Date toLocaleString 方法:将 Date 对象转化为本地化格式显示
  18. 18. JavaScript Date toLocaleDateString 与 toLocaleTimeString 方法
腾讯云618限量抢购