Moment.js moment().endOf() 方法
moment().endOf() 方法 用于修改 Moment,使其设置为给定时间单位的末尾。可用的时间单位有年、月、季度、周、日、小时、分钟和秒。
语法:
moment().endOf( String );
参数: 此方法接受一个参数,如上所述,并在下面进行描述:
- String: 它是将Moment对象设置为结束的时间单位。
返回值: 此方法返回设置后的变异Moment。
注意: 在普通的Node.js程序中,这将不起作用,因为它需要在全局范围或项目目录中安装外部的moment.js库。
可以使用以下命令安装Moment.js:
安装moment模块:
npm install moment
下面的示例将演示 Moment.js 的 moment().endOf() 方法。
示例1:
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:
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/