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


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


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

用法:

DateObj.setUTCMinutes(Minutes_Value);

DateObj是使用Date()构造函数创建的有效Date对象,我们要在其中设置分钟。分钟的值是从0到59。


参数:这里的参数minutes_Value是我们要在使用Date()构造函数创建的日期中设置的分钟数。

返回值:它返回由setUTCMinutes()函数设置的新的即更新的分钟。

下面的程序演示了setUTCMinutes()函数:

<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 minute of 52 is being set in above Date 
// Object with the help of setUTCMinutes() function 
dateobj.setUTCMinutes(52); 
  
// new minute from above Date Object is 
// being extracted using getUTCMinutes() 
var B = dateobj.getUTCMinutes(); 
  
// Printing new minute 
document.write(B); 
</script>

输出:

52

错误和异常

  • 示例1:如果在Date()构造函数中创建日期对象时我们没有给出任何分钟,则setUTCMinutes()函数仍然可以根据创建的Date对象中的通用时间设置新的分钟。
    <script> 
    // Here minute has not been assigned according to 
    // universal time while creating Date object 
    var dateobj = new Date('October 13, 1996 GMT-3:00'); 
      
    // new minute of 51 is being set in above Date 
    // Object with the help of setUTCMinutes() function 
    dateobj.setUTCMinutes(51); 
      
    // new minute from above Date Object is 
    // being extracted using getUTCMinutes() 
    var B = dateobj.getUTCMinutes(); 
      
    // Printing new minute 
    document.write(B); 
    </script>

    输出:

    51
  • 示例2:如果在Date()构造函数中未提供任何参数,则setUTCMinutes()函数仍可以设置分钟,但月份,年份,日期等仍为当前值。
    <script> 
    // Here nothing has been assigned according to 
    // universal time while creating Date object 
    var dateobj = new Date(); 
      
    // new minute of 42 is being set in above Date 
    // Object with the help of setUTCMinutes() function 
    dateobj.setUTCMinutes(42); 
      
    // Minutes from above Date Object is 
    // being extracted using getUTCMinutes() 
    var B = dateobj.getUTCMinutes(); 
      
    // month from above Date Object is 
    // being extracted using getUTCMonth() 
    var C = dateobj.getUTCMonth(); 
      
    // date from above Date Object is 
    // being extracted using getUTCDate() 
    var D = dateobj.getUTCDate(); 
      
    // year from above Date Object is 
    // being extracted using getUTCFullYear() 
    var E = dateobj.getUTCFullYear(); 
      
    // Printing new minutes 
    document.write(B + "<br>"); 
      
    // Printing current month 
    document.write(C + "<br>"); 
      
    // Printing current date 
    document.write(D + "<br>"); 
      
    // Printing current year 
    document.write(E); 
    </script>

    输出:

    42
    3
    1
    2018

    这里42是新分钟,3是当前月,即4月,1是当前日期,2018是当前年。

  • 示例3:如果将分钟值66设置为setUTCMinutes()函数的参数,则会将6设置为分钟,因为分钟范围是从0到59,因此66-60=6,此处减去60,因为0到59为60。
    <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 minute of 66 is being set in above Date 
    // Object with the help of setUTCMinutes() function 
    dateobj.setUTCMinutes(66); 
      
    // minute from above Date Object is 
    // being extracted using getUTCMinutes() 
    var B = dateobj.getUTCMinutes(); 
      
    // Hour from above Date Object is 
    // being extracted using getUTCHours() 
    var C = dateobj.getUTCHours(); 
      
    // Printing new minute 
    document.write(B + "<br>"); 
      
    // Printing hour 
    document.write(C); 
    </script>

    输出:

    6
    6

支持的浏览器:JavaScript date.setUTCMinutes()函数支持的浏览器如下:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • Opera
  • 苹果浏览器


相关用法


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