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


JavaScript Date setUTCDate()用法及代码示例


下面是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




相关用法


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