当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Javascript date.setUTCDate()用法及代码示例


date.setUTCDate()是JavaScript中的内置函数,用于根据通用时间将一个月的日期设置为使用Date()构造函数创建的日期对象。

用法:

DateObj.setUTCDate(date_Value);

DateObj是使用Date()构造函数创建的有效Date对象,在该构造函数中,我们要根据通用时间设置一个月的日期。


参数:这里的参数date_Value是要在使用Date()构造函数创建的日期对象中设置的日期值。

返回值:它返回由setUTCDate()函数设置的新的月更新日期,该月的日期是1到31之间的整数值。

以下示例程序旨在说明setITCDate()函数:

<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() function 
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

错误和异常

  • 示例1:在这里,我们知道月份的日期在1到31之间,但是如果我们尝试将日期设置为33,则它将下个月的日期设置为2,因为33-31 = 2,并且此日期2成为下一个日期上个月的。
    <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是十一月的日期,而10是十一月的月份,因为月份名称从0到11开始,即,一月为0,十二月为11。

  • 示例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() function 
    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() function 
    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() function 
    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
  • 苹果浏览器


相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript | date.setUTCDate() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。