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


Moment.js moment().endOf()用法及代码示例

moment().endOf() 方法用于改变 Moment,以便将其设置为给定时间单位的末尾。可用的时间单位可以是年、月、季度、周、日、小时、分钟和秒。

用法:

moment().endOf( String );

Parameters: 该方法接受如上所述和如下所述的单个参数:

  • String: 它是将 Moment 对象设置为结束的时间单位。

返回值:此方法返回设置值后的变异 Moment。

注意:这在普通的 Node.js 程序中不起作用,因为它需要全局安装或在项目目录中安装外部moment.js 库。

Moment.js 可以使用以下命令安装:

moment模块的安装:

npm install moment

以下示例将演示 Moment.js moment().endOf() 方法。

示例 1:

Javascript


const moment = require('moment'); 
  
console.log("Current moment is:", moment().toString()); 
  
console.log( 
    "endOf current moment's day:", 
    moment().endOf('day').toString() 
) 
  
console.log( 
    "endOf current moment's week:", 
    moment().endOf('week').toString() 
) 
  
console.log( 
    "endOf current moment's month:", 
    moment().endOf('month').toString() 
) 
  
console.log( 
    "endOf current moment's quarter:", 
    moment().endOf('quarter').toString() 
) 
  
console.log( 
    "endOf current moment's year:", 
    moment().endOf('year').toString() 
)

输出:

Current moment is: Mon Jul 18 2022 02:36:03 GMT+0530
endOf current moment’s day: Mon Jul 18 2022 23:59:59 GMT+0530
endOf current moment’s week: Sat Jul 23 2022 23:59:59 GMT+0530
endOf current moment’s month: Sun Jul 31 2022 23:59:59 GMT+0530
endOf current moment’s quarter: Fri Sep 30 2022 23:59:59 GMT+0530
endOf current moment’s year: Sat Dec 31 2022 23:59:59 GMT+0530

示例 2:

Javascript


const moment = require('moment'); 
  
console.log("Current moment is:", 
    moment().toString()); 
  
console.log( 
    "endOf current moment's second:", 
    moment().endOf('second').toString() 
) 
  
console.log( 
    "endOf current moment's minute:", 
    moment().endOf('minute').toString() 
) 
  
console.log( 
    "endOf current moment's hour:", 
    moment().endOf('hour').toString() 
)

输出:

Current moment is: Mon Jul 18 2022 02:36:03 GMT+0530
endOf current moment’s second: Mon Jul 18 2022 02:36:03 GMT+0530
endOf current moment’s minute: Mon Jul 18 2022 02:36:59 GMT+0530
endOf current moment’s hour: Mon Jul 18 2022 02:59:59 GMT+0530

参考: https://momentjs.com/docs/#/manipulating/end-of/



相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Moment.js moment().endOf() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。