下面是一个例子 日期设置日期() 方法
null
- 例子:
<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
这个 日期setUTCDate() 方法用于根据通用时间将一个月的日期设置为日期对象,该对象是使用date()构造函数创建的。
语法:
DateObj.setUTCDate(date_Value);
参数: 此方法接受一个参数,如上所述,如下所述:
- 日期值 这个参数包含我们想要在使用date()构造函数创建的date对象中设置的date值。
返回值: 它返回由setUTCDate()方法设置的月份的新日期,即更新日期。月份的日期是1到31之间的整数值。
以上方法的更多代码如下:
项目1: 我们知道,这个月的日期在1到31之间,但如果我们尝试将日期设置为33,它会将下个月的日期设置为2,因为33-31=2,这个日期2将成为上个月下一个月的日期。
在上面的输出中,2是11月份的日期,10是11月份,因为月份名称从0开始到11,即0代表1月份,11代表12月份。
<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()构造函数中,我们在创建Date对象时没有给出月份的任何日期,那么setUTCDate()方法将能够设置新的日期,该日期在创建的Date对象中作为参数给出。
<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()构造函数中没有给出任何参数,still 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()方法 以下列出了:
- 谷歌Chrome 1及以上版本
- 边缘12及以上
- Firefox 1及以上版本
- Internet Explorer 4及以上版本
- 歌剧4及以上
- Safari 1及以上
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END