Moment.js moment().toArray() 方法
moment().toArray() 方法 用于返回一个类似于new Date()对象参数的数组。数组包含年、月、日、时、分、秒和毫秒的值。
语法:
moment().toArray();
参数: 此方法不接受任何参数:
返回值: 此方法以JSON格式返回持续时间。
注意: 这在普通的Node.js程序中不起作用,因为它需要全局安装或在项目目录中安装一个外部的moment.js库。
Moment.js可以使用以下命令进行安装:
moment模块的安装:
npm install moment
下面的示例将演示Moment.js的 moment().toArray() 方法。
示例1:
const moment = require('moment');
let momentOne = moment();
console.log(
"MomentOne toArray():", momentOne.toArray()
)
let momentTwo = moment("01-08-2022", "MM-DD-YYYY");
console.log(
"MomentTwo toArray():", momentTwo.toArray()
)
let momentThree = moment("10:25:20", "hh:mm:ss");
console.log(
"MomentThree toArray():", momentThree.toArray()
)
输出:
MomentOne toArray(): [
2022, 5, 28,
23, 34, 58,
562
]
MomentTwo toArray(): [
2022, 0, 8, 0,
0, 0, 0
]
MomentThree toArray(): [
2022, 5, 28,
10, 25, 20,
0
]
示例2:
const moment = require('moment');
let moment1 = moment().year(2021);
console.log(
"Moment1 toArray():", moment1.toArray()
)
let moment2 = moment1.add(10, 'months');
console.log(
"Moment2 toArray():", moment2.toArray()
)
let moment3 = moment2.add(20, 'days');
console.log(
"Moment3 toArray():", moment3.toArray()
)
输出:
Moment1 toArray(): [
2021, 5, 28,
23, 34, 58,
577
]
Moment2 toArray(): [
2022, 3, 28,
23, 34, 58,
577
]
Moment3 toArray(): [
2022, 4, 18,
23, 34, 58,
577
]
参考: **** https://momentjs.com/docs/#/displaying/as-array/
极客教程