CoffeeScript 日期 - setSeconds()



说明

setSeconds() 方法根据本地时间设置指定日期的秒数

语法

下面是 setSeconds() 方法的语法。

Date.setSeconds(secondsValue[, msValue])

参数详情

  • secondsValue - 0 到 59 之间的整数。

  • msValue - 0 到 999 之间的数字,表示毫秒。

如果未指定 msValue 参数,则使用 getMilliseconds 方法返回的值。如果指定的参数超出了预期范围,setSeconds 会尝试相应地更新 Date 对象中的日期信息。例如,如果 secondsValue 使用 100,则 Date 对象中存储的分钟将增加 1,并将使用 40 表示秒。

示例

以下示例演示了 CoffeeScript 中 setSeconds() 方法的使用。将此代码保存在名为 date_setseconds.coffee 的文件中。

dt = new Date "February 19, 2016 23:15:00"
dt.setSeconds 25
console.log dt

打开 命令提示符,并编译 .coffee 文件,如下所示。

c:\> coffee -c date_setseconds.coffee

在编译时,它会生成以下 JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var dt;

  dt = new Date("February 19, 2016 23:15:00");

  dt.setSeconds(25);

  console.log(dt);

}).call(this);

现在,再次打开 命令提示符,并运行 CoffeeScript 文件,如下所示。

c:\> coffee date_setseconds.coffee

执行后,CoffeeScript 文件会生成以下输出。

Fri Feb 19 2016 23:15:25 GMT+0530 (India Standard Time)
coffeescript_date.htm
广告
© . All rights reserved.