JavaScript中的Date.setUTCSeconds()函数

Date对象是JavaScript语言中内置的数据类型。日期对象使用新的Date()创建,如下所示。

创建Date对象后,可以使用多种方法对其进行操作。大多数方法仅允许您使用本地时间或UTC(通用或GMT)时间来获取和设置对象的年,月,日,时,分,秒和毫秒字段。

date对象的setUTCSeconds()函数接受表示秒的整数,并根据通用时间用其替换当前日期(时间)中的秒值。

语法

其语法如下

dateObj.setUTCSeconds();

示例

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 89 12:4:25:96');
      document.write("Current date: "+dateObj.toUTCString());
      document.write("<br>");
      dateObj.setUTCSeconds(34);
      document.write("Date after setting new seconds: "+dateObj.toUTCString());
   </script>
</body>
</html>

输出结果

Current date: Tue, 26 Sep 1989 06:34:25 GMT
Date after setting new seconds: Tue, 26 Sep 1989 06:34:34 GMT

示例

尽管在创建日期对象时没有提到一天中的几秒钟,但仍可以setSeconds()根据世界时间使用该功能进行设置。

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('26, September, 1989 4:25');
      dateObj.setSeconds(55);
      document.write(dateObj.toString());
   </script>
</body>
</html>

输出结果

Tue Sep 26 1989 04:25:55 GMT+0530 (India Standard Time)

示例

同样,尽管在创建日期对象时未将任何值传递给构造函数,但仍可以使用此函数根据通用时间以及秒,月,日,年和其他值来设置秒数。当前日期(和时间)。

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date();
      dateObj.setSeconds(55);
      document.write(dateObj.toString());
   </script>
</body>
</html>

输出结果

Thu Oct 18 2018 18:44:55 GMT+0530 (India Standard Time)