下麵是Date setUTCDate()方法的示例。
- 例:
<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 date 15 of the month is being set in above // Date Object with the help of setDate() method dateobj.setUTCDate(15); // New date of the month according to universal // time from above Date Object is being extracted // using getDate() var B = dateobj.getUTCDate(); // Printing new date of the month document.write(B); </script>
- 輸出:
15
date.setUTCDate()方法用於根據通用時間將一個月的日期設置為使用Date()構造函數創建的日期對象。
用法:
DateObj.setUTCDate(date_Value);
參數:該方法接受上述和以下描述的單個參數:
- date_Value此參數保存我們要在使用Date()構造函數創建的date對象中設置的date值。
返回值:它返回由setUTCDate()方法設置的新的月更新日期。月份的日期是一個整數值,範圍是1到31。
上述方法的更多代碼如下:
程序1:在這裏,我們知道該月的日期在1到31之間,但是如果我們嘗試將日期設置為33,則它將下個月的日期設置為2,因為33-31 = 2並且此日期2成為日期上個月的下個月。
在上麵的輸出中,2是11月的日期,10是11月,因為月份名稱從0到11開始,即1月為0,12月為11。
<script>
// Here a date according to universal time has
// been assigned while creating Date object
var dateobj =
new Date('October 13, 1996 05:35:32 GMT-3:00');
// New date 33 of the month is being set in above
// Date Object with the help of setUTCDate() function
dateobj.setUTCDate(33);
// New date of the month from above Date Object is
// being extracted using getUTCDate()
var B = dateobj.getUTCDate();
// New month from above Date Object is
// being extracted using getUTCMonth()
var C = dateobj.getUTCMonth();
// Printing new date of the month
document.write(B + "<br />");
// Printing new month
document.write(C);
</script>
輸出:
2 10
程序2:如果在Date()構造函數中我們在創建日期對象時不提供月份的任何日期,則setUTCDate()方法仍然可以設置新日期,該日期作為其在創建的日期對象中的參數給出。
<script>
// Here date according to universal time has
// not been assigned while creating Date object
var dateobj =
new Date('October, 1996 05:35:32 GMT-3:00');
// New date 12 of the month is being set in above Date
// Object with the help of setUTCDate() method
dateobj.setUTCDate(12);
// New date of the month from above Date Object is
// being extracted using getUTCDate()
var B = dateobj.getUTCDate();
// Printing new date of the month
document.write(B);
</script>
輸出:
12
程序3:如果Date()構造函數中未提供任何參數,則setDate()函數仍可以設置日期,而月份仍為當前日期。
<script>
// Here nothing has been assigned
// while creating Date object
var dateobj = new Date();
// new date 13 of the month is being set in above
// Date Object with the help of setUTCDate() method
dateobj.setUTCDate(13);
// Date of the month from above Date Object is
// being extracted using getUTCDate()
var B = dateobj.getUTCDate();
// Month from above Date Object is
// being extracted using getUTCMonth()
var C = dateobj.getUTCMonth();
// Printing new date of the month
document.write(B + "<br />");
// Printing new month
document.write(C);
</script>
輸出:
13 3
程序4:如果將月份的新日期設置為零(0),則新日期將設置為上個月的最後一天。
<script>
// Here a date according to universal time
// has been assigned while creating Date object
var dateobj =
new Date('October 13, 1996 05:35:32 GMT-3:00');
// New date 0 of the month is being set in above
// Date Object with the help of setUTCDate() method
dateobj.setUTCDate(0);
// Date of the month from above Date Object is
// being extracted using getUTCDate()
var B = dateobj.getUTCDate();
// Month from above Date Object is
// being extracted using getUTCMonth()
var C = dateobj.getUTCMonth();
// Printing new date of the month
document.write(B + "<br>");
// Printing new month
document.write(C);
</script>
輸出:
30 8
支持的瀏覽器:下麵列出了JavaScript Date setUTCDate()方法支持的瀏覽器:
- 穀歌瀏覽器
- 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 setUTCDate() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。