下麵是Date setSeconds()方法的示例。
- 例:
<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() method 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
date.setUTCMonth()方法用於根據通用時間將月份設置為使用date()構造函數創建的日期對象。句法:
dateObj.setUTCMonth(month_Value);
參數:該方法接受上述和以下描述的單個參數:
- month_Value此參數保存用於在date()構造函數中設置的month的值。
返回值:它返回由setUTCMonth()方法設置的新的即更新的月份。
注意:dateObj是使用Date()構造函數創建的有效Date對象,我們要在其中構造月份。
上述方法的更多代碼如下:
程序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() method
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()方法設置為月,但年,日期等根據通用時間變為當前的。
根據通用時間,此處11是12月的新月,1是當前日期,而2018是當前年份。
<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() method
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
程序3:如果將setUTCMonth()方法的參數指定為month 15,則將3設置為month,因為month範圍是0到11,因此,這裏減去12是因為0到11為12。
這裏3是4月的新月,因為月份範圍是0到11,即總計12,所以年份從1996年變為1997,我們將新月份設置為3,從1996年起將年份增加1到1997,月份變成3到世界時間。
<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() method
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
支持的瀏覽器:下麵列出了JavaScript Date setUTCMonth()方法支持的瀏覽器:
- 穀歌瀏覽器
- IE瀏覽器
- 火狐瀏覽器
- Opera
- Safari
相關用法
- JavaScript Date toLocaleTimeString()用法及代碼示例
- JavaScript Date UTC()用法及代碼示例
- JavaScript Date getHours()用法及代碼示例
- Javascript date.getMinutes()用法及代碼示例
- Javascript date.getMonth()用法及代碼示例
- Javascript date.getSeconds()用法及代碼示例
- Javascript date.getDay()用法及代碼示例
- JavaScript Date getMilliseconds()用法及代碼示例
- JavaScript Date getFullYear()用法及代碼示例
- JavaScript Date getDate()用法及代碼示例
- JavaScript Date getUTCDate()用法及代碼示例
- JavaScript Date getUTCDay()用法及代碼示例
- JavaScript Date getUTCFullYear()用法及代碼示例
- JavaScript Date getUTCHours()用法及代碼示例
- JavaScript Date getUTCMilliseconds()用法及代碼示例
- JavaScript Date getUTCSeconds()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript Date setUTCMonth() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。