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.