date.setUTCMonth()是JavaScript中的內置函數,用於根據通用時間將月份設置為使用date()構造函數創建的日期對象。
用法:
dateObj.setUTCMonth(month_Value);
DateObj是使用Date()構造函數創建的有效Date對象,我們要在其中構造月份。月份的值從0到11,因為一年中從一月到十二月的月份總數是12。值0用於一月,1用於二月,依此類推,直到12月11。
參數:這裏的參數是month_Value,即用於在date()構造函數中設置的month的值。
返回值:它返回由setUTCMonth()函數設置的新的即更新的月份。
<script>
// Here a date has been assigned according to
// universal time while creating Date object
var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00');
// new month of January is being set in above Date
// Object with the help of setUTCMonth() function
dateobj.setUTCMonth(0);
// new month from above Date Object is
// being extracted using getUTCMonth()
var B = dateobj.getMonth();
// Printing new month
document.write(B);
</script>
輸出:
0
錯誤和異常
- 示例1:如果在Date()構造函數中沒有給出任何月份,則setUTCMonth()函數仍設置新的月份作為其參數。
<script> // Here month has not been assigned according to // universal time while creating Date object var dateobj = new Date('1996, 05:35:32 GMT-3:00'); // new month of 2 is being set in above Date // Object with the help of setUTCMonth() function dateobj.setUTCMonth(2); // new month from above Date Object is // being extracted using getUTCMonth() var B = dateobj.getUTCMonth(); // Printing new month document.write(B); </script>
輸出:
2
- 示例2:如果Date()構造函數中未提供任何參數作為參數,則setUTCMonth()函數仍將設置月份,但年,日期等將根據通用時間變為當前時間。
<script> // Here nothing has been assigned according to // universal time while creating Date object var dateobj = new Date(); // new month of 11 is being set in above Date // Object with the help of setUTCMonth() function dateobj.setUTCMonth(11); // Month from above Date Object is // being extracted using getUTCMonth() var B = dateobj.getUTCMonth(); // date from above Date Object is // being extracted using getUTCDate() var C = dateobj.getUTCDate(); // year from above Date Object is // being extracted using getUTCFullYear() var D = dateobj.getUTCFullYear(); // Printing new month document.write(B + '<br>'); // Printing current date document.write(C + '<br>'); // Printing current year document.write(D + '<br>'); </script>
輸出:
11 1 2018
根據通用時間,此處11是12月的新月,1是當前日期,而2018是當前年份。
- 示例3:如果將月15的值作為setUTCMonth()函數的參數給出,則會將3設置為月,因為月範圍是0到11,因此,這裏減去12是因為0到11為12。
<script> // Here date has been assigned according to // universal time while creating Date object var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00'); // new month of 15 is being set in above Date // Object with the help of setUTCMonth() function dateobj.setUTCMonth(15); // month from above Date Object is // being extracted using getUTCMonth() var B = dateobj.getUTCMonth(); // year from above Date Object is // being extracted using getUTCFullYear() var C = dateobj.getUTCFullYear(); // Printing new month document.write(B + '<br>'); // Printing new year document.write(C); </script>
輸出:
3 1997
這裏3是4月的新月,因為月份範圍是從0到11,即總數為12,所以從1996年成為1997年,我們將新月份設置為3,從1996年到1997年增加一年,月份根據通用時間變成3 。
支持的瀏覽器:JavaScript date.setUTCMonth()函數支持的瀏覽器如下:
- 穀歌瀏覽器
- IE瀏覽器
- 火狐瀏覽器
- Opera
- 蘋果瀏覽器
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | date.setUTCMonth()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。