當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


CoffeeScript Date setMonth()用法及代碼示例


描述

這個setMonth()方法根據本地時間設置指定日期的月份。

用法

下麵給出的是setMonth()方法。

Date.setMonth(monthValue[, dayValue])

參數詳情

  • monthValue- 0 到 11 之間的整數(代表一月到十二月)。

  • dayValue- 一個從 1 到 31 的整數,代表月份中的第幾天。

如果未指定 dayValue 參數,則使用從 getDate 方法返回的值。如果您指定的參數超出預期範圍,則 setMonth 會相應地嘗試更新 Date 對象中的日期信息。例如,如果您使用 15 作為 monthValue,則年份將增加 1(年 + 1),而 3 將用於月份。

示例

下麵的例子演示了setMonth()CoffeeScript 中的方法。將此代碼保存在具有名稱的文件中date_setmonth.coffee

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

打開command prompt並編譯 .coffee 文件,如下所示。

c:\> coffee -c date_setmonth.coffee

在編譯時,它會為您提供以下 JavaScript。

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

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

  dt.setMonth(5);

  console.log(dt);

}).call(this);

現在,打開command prompt再次運行 CoffeeScript 文件,如下所示。

c:\> coffee date_setmonth.coffee

在執行時,CoffeeScript 文件產生以下輸出。

Sun Jun 19 2016 23:15:00 GMT+0530 (India Standard Time)

相關用法


注:本文由純淨天空篩選整理自 CoffeeScript Date - setMonth()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。