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


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


下面是Date setSeconds()方法的示例。

  • 例:
    <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 month of January is being set in above Date 
      // Object with the help of setUTCMonth() method 
      dateobj.setUTCMonth(0); 
      
      // New month from above Date Object is 
      // being extracted using getUTCMonth() 
      var B = dateobj.getMonth(); 
      
      // Printing new month 
      document.write(B); 
                             
    </script>                        
  • 输出:
    0

date.setUTCMonth()方法用于根据通用时间将月份设置为使用date()构造函数创建的日期对象。句法:

dateObj.setUTCMonth(month_Value);

参数:该方法接受上述和以下描述的单个参数:

  • month_Value此参数保存用于在date()构造函数中设置的month的值。

返回值:它返回由setUTCMonth()方法设置的新的即更新的月份。

注意:dateObj是使用Date()构造函数创建的有效Date对象,我们要在其中构造月份。



上述方法的更多代码如下:

程序1:如果在Date()构造函数中没有给出任何月份,则setUTCMonth()方法仍然设置新的月份作为其参数。

<script> 
  // Here month has not been assigned according to 
  // universal time while creating Date object 
  var dateobj = new Date('1996, 05:35:32 GMT-3:00'); 
  
  // New month of 2 is being set in above Date 
  // Object with the help of setUTCMonth() method 
  dateobj.setUTCMonth(2); 
  
  // New month from above Date Object is 
  // being extracted using getUTCMonth() 
  var B = dateobj.getUTCMonth(); 
  
  // Printing new month 
  document.write(B); 
</script>

输出:

2

程序2:如果在Date()构造函数中未提供任何参数作为参数,则仍将setUTCMonth()方法设置为月,但年,日期等根据通用时间变为当前的。
根据通用时间,此处11是12月的新月,1是当前日期,而2018是当前年份。

<script> 
  // Here nothing has been assigned according to 
  // universal time while creating Date object 
  var dateobj = new Date(); 
  
  // New month of 11 is being set in above Date 
  // Object with the help of setUTCMonth() method 
  dateobj.setUTCMonth(11); 
  
  // Month from above Date Object is 
  // being extracted using getUTCMonth() 
  var B = dateobj.getUTCMonth(); 
  
  // Date from above Date Object is 
  // being extracted using getUTCDate() 
  var C = dateobj.getUTCDate(); 
  
  // Year from above Date Object is 
  // being extracted using getUTCFullYear() 
  var D = dateobj.getUTCFullYear(); 
  
  // Printing new month 
  document.write(B + '<br />'); 
  
  // Printing current date 
  document.write(C + '<br />'); 
  
  // Printing current year 
  document.write(D + '<br />'); 
                          
</script>                        

输出:

11
1
2018

程序3:如果将setUTCMonth()方法的参数指定为month 15,则将3设置为month,因为month范围是0到11,因此15-12=3,这里减去12是因为0到11为12。
这里3是4月的新月,因为月份范围是0到11,即总计12,所以年份从1996年变为1997,我们将新月份设置为3,从1996年起将年份增加1到1997,月份变成3到世界时间。

<script> 
  // Here 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 month of 15 is being set in above Date 
  // Object with the help of setUTCMonth() method 
  dateobj.setUTCMonth(15); 
  
  // Month from above Date Object is 
  // being extracted using getUTCMonth() 
  var B = dateobj.getUTCMonth(); 
  
  // Year from above Date Object is 
  // being extracted using getUTCFullYear() 
  var C = dateobj.getUTCFullYear(); 
  
  // Printing new month 
  document.write(B + '<br />'); 
  
  // Printing new year 
  document.write(C); 
                         
</script>                        

输出:

3
1997

支持的浏览器:下面列出了JavaScript Date setUTCMonth()方法支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • Opera
  • Safari




相关用法


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