date.setDate()是JavaScript中的內置函數,用於將一個月的日期設置為使用date()構造函數創建的日期對象。句法:
dateObj.setDate(date_Value);
DateObj是使用Date()構造函數創建的有效Date對象,我們要在其中設置一個月的日期。
參數:這裏的參數是date_Value,即用於在date()構造函數中設置的date的值。
返回值:它返回由setDate()函數設置的新的月更新日期。月份的日期是一個整數值,範圍是1到31。
<script>
// Here a date has been assigned
// while creating Date object
var dateobj = new Date('October 13, 1996 05:35:32');
// new date 15 of the month is being set in above Date
// Object with the help of setDate() function
dateobj.setDate(15);
// new date of the month from above Date Object is
// being extracted using getDate()
var B = dateobj.getDate();
// Printing new date of the month
document.write(B);
</script>
輸出:
15
錯誤和異常
- 範例1:在這裏,我們知道該月的日期在1到31之間,但是如果我們假設將日期設置為33,它將為下個月設置日期2,因為
並且此日期2成為上個月下一個日期。
<script> // Here a date has been assigned // while creating Date object var dateobj = new Date('October 13, 1996 05:35:32'); // new date 33 of the month is being set in above Date // Object with the help of setDate() function dateobj.setDate(33); // new date of the month from above Date Object is // being extracted using getDate() var B = dateobj.getDate(); // new month from above Date Object is // being extracted using getMonth() var C = dateobj.getMonth(); // Printing new date of the month document.write(B); // Printing new month document.write(C); </script>
輸出:
2 10
在上麵的輸出中,2是十一月的日期,而10是十一月的月份,因為月份名稱從0到11開始,即,一月為0,十二月為11。
- 範例2:如果在Date()構造函數中我們不提供月份的任何日期,則setDate()函數仍設置新日期作為其參數。
<script> // Here date has not been assigned // while creating Date object var dateobj = new Date('October, 1996 05:35:32'); // new date 12 of the month is being set in above Date // Object with the help of setDate() function dateobj.setDate(12); // new date of the month from above Date Object is // being extracted using getDate() var B = dateobj.getDate(); // Printing new date of the month document.write(B); </script>
輸出:
12
- 範例3:如果Date()構造函數中未提供任何參數作為參數,則setDate()函數仍設置日期,但月份變為當前月份。
<script> // Here a date 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 setDate() function dateobj.setDate(13); // date of the month from above Date Object is // being extracted using getDate() var B = dateobj.getDate(); // month from above Date Object is // being extracted using getMonth() var C = dateobj.getMonth(); // Printing new date of the month document.write(B + "<br>"); // Printing new month document.write(C); </script>
輸出:
13 2
- 範例4:如果該月的新日期設置為零(0),則新日期將設置為上個月的最後一天。
<script> // Here a date has been assigned // while creating Date object var dateobj = new Date('October 13, 1996 05:35:32'); // new date 0 of the month is being set in above Date // Object with the help of setDate() function dateobj.setDate(0); // date of the month from above Date Object is // being extracted using getDate() var B = dateobj.getDate(); // month from above Date Object is // being extracted using getMonth() var C = dateobj.getMonth(); // Printing new date of the month document.write(B + "<br>"); // Printing new month document.write(C); </script>
輸出:
30 8
支持的瀏覽器:JavaScript date.setDate()函數支持的瀏覽器如下:
- 穀歌瀏覽器
- IE瀏覽器
- 火狐瀏覽器
- Opera
- 蘋果瀏覽器
相關用法
- Javascript Math.abs( )用法及代碼示例
- Javascript toPrecision()用法及代碼示例
- Javascript toExponential()用法及代碼示例
- Javascript Math.pow( )用法及代碼示例
- Javascript toString()用法及代碼示例
- Javascript toFixed()用法及代碼示例
- Javascript Math.E()用法及代碼示例
- Javascript Array.of()用法及代碼示例
- Javascript Array every()用法及代碼示例
- Javascript Array some()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | date.setDate() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。