當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaScript Date getMonth()用法及代碼示例

JavaScript 日期 getMonth() 方法

getMonth() 方法是 Date 的類方法,用於從當前日期獲取月份的值。

它不接受任何參數並返回 0 到 11 之間的值。

注意:返回值是從 0 到 11,0 為 1 月,1 為 2 月,...,11 為 12 月

用法:

    var dt = new Date();
    dt.getMonth();

例子:

    Input:
    var dt = new Date();
    dt.getMonth();
    
    Output:
    2 (if current month is March)

使用 getMonth() 方法從當前日期獲取月份值的 JavaScript 代碼

<html>
<head><title>JavaScipt Example</title></head>

<body>
<script>
	var dt = new Date(); //Date constructor 
	var month = dt.getMonth(); //getting month from the current date
	//printing month
	document.write("Current month is:" + month);
</script>
</body>
</html>

輸出

Current month is:2



相關用法


注:本文由純淨天空篩選整理自 Date getMonth() method with example in JavaScript。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。