当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


CoffeeScript Date setUTCFullYear()用法及代码示例


描述

这个setUTCFullYear()方法根据通用时间设置指定日期的全年。

用法

下面给出的是setUTCFullYear()方法。

Date.setUTCFullYear(yearValue[, monthValue[, dayValue]])

参数详情

  • yearValue− 一个整数,指定年份的数值,例如 2008。

  • monthValue- 0 到 11 之间的整数,表示一月到十二月。

  • dayValue- 一个介于 1 和 31 之间的整数,表示月份中的某天。如果指定 dayValue 参数,则还必须指定 monthValue。

如果不指定monthValue 和dayValue 参数,则使用从getMonth 和getDate 方法返回的值。如果您指定的参数超出预期范围,setUTCFullYear 会相应地尝试更新其他参数和 Date 对象中的日期信息。例如,如果将monthValue 指定为15,则年份加1(年份+1),月份使用3。

示例

下面的例子演示了 setUTCFullYear()CoffeeScript 中的方法。将此代码保存在具有名称的文件中date_setutcfullYear.coffee

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

打开command prompt并编译 .coffee 文件,如下所示。

c:\> coffee -c date_setutcfullYear.coffee

在编译时,它会为您提供以下 JavaScript。

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

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

  dt.setUTCFullYear(20);

  console.log(dt);

}).call(this);

现在,打开command prompt再次运行 CoffeeScript 文件,如下所示。

c:\> coffee date_setutcfullYear.coffee

在执行时,CoffeeScript 文件产生以下输出。

Wed Feb 19 20 23:15:00 GMT+0530 (India Standard Time)

相关用法


注:本文由纯净天空筛选整理自 CoffeeScript Date - setUTCFullYear()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。