下面是一个例子 日期设置秒() 方法
null
- 例子:
javascript
<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 second of 52 is being set in above Date
// Object with the help of setUTCSeconds() method
dateobj.setUTCSeconds(52);
// New second from above Date Object is
// being extracted using getUTCSeconds()
var
B = dateobj.getUTCSeconds();
// Printing new Second
document.write(B);
</script>
- 输出:
52
这个 日期setUTCSeconds() 方法用于根据通用时间将秒设置为日期对象,该对象是使用date()构造函数创建的。
语法:
DateObj.setUTCSeconds(seconds_Value);
参数: 该方法接受一个参数,如上所述,如下所述:
- 秒值: 此参数保存第二个值,我们希望在使用date()构造函数创建的日期中设置该值。
返回值: 它返回由setUTCSeconds()方法设置的新的即更新秒数。
注: 这个 DateObj 是使用Date()构造函数创建的有效日期对象,我们希望在其中设置第二个。秒的值从0到59。
以上方法的更多代码如下:
项目1: 如果在Date()构造函数中,我们在创建Date对象时没有给出任何秒,那么setUTCSeconds()方法将能够根据创建的Date对象中的通用时间设置新秒作为其参数。
javascript
<script> // Here second has not been assigned according to // universal time while creating Date object var dateobj = new Date( 'October 13, 1996 GMT-3:00' ); // New second of 51 is being set in above Date // Object with the help of setUTCSeconds() method dateobj.setUTCSeconds(51); // New second from above Date Object is // being extracted using getUTCSeconds() var B = dateobj.getUTCSeconds(); // Printing new Second document.write(B); </script> |
输出:
51
项目2: 如果Date()构造函数中没有给出任何参数,那么setUTCSeconds()方法将能够设置秒,但月份、年份、日期等仍然是当前的。 这里42是新的第二个,3是当前月份,即4月,1是当前日期,2018年是世界时的当前年份。
javascript
<script> // Here nothing has been assigned // while creating Date object var dateobj = new Date(); // New second of 42 is being set in above Date // Object with the help of setUTCSeconds() method dateobj.setUTCSeconds(42); // Seconds from above Date Object is // being extracted using getUTCSeconds() var B = dateobj.getUTCSeconds(); // 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 seconds 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
方案3: 如果second 66的值作为setSeconds()方法的参数给出,它会将6设置为second,因为第二个范围是0到59,因此 ,这里减去60,因为0到59是60。
javascript
<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 second of 66 is being set in above Date // Object with the help of setUTCSeconds() method dateobj.setUTCSeconds(66); // Seconds from above Date Object is // being extracted using getUTCSeconds() var B = dateobj.getUTCSeconds(); // Minute from above Date Object is // being extracted using getUTCMinutes() var C = dateobj.getUTCMinutes(); // Printing new seconds document.write(B + "<br>" ); // Printing minutes document.write(C); </script> |
输出:
6 36
支持的浏览器: 支持的浏览器 JavaScript Date setUTCSeconds()方法 以下列出了:
- 谷歌Chrome 1及以上版本
- 边缘12及以上
- Firefox 1及以上版本
- Internet Explorer 4及以上版本
- 歌剧4及以上
- Safari 1及以上
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END