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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。