moment().toArray() 方法用於返回一個類似於新 Date() 對象的參數的數組。該數組包含年、月、日、小時、分鍾、秒和毫秒的值。
用法:
moment().toArray();
Parameters: 該方法不接受任何參數:
返回值:此方法以 JSON 格式返回持續時間。
注意:這在普通的 Node.js 程序中不起作用,因為它需要外部 moment.js 庫
要全局安裝或安裝在項目目錄中。
Moment.js 可以使用以下命令安裝:
moment模塊的安裝:
npm install moment
以下示例將演示 Moment.js moment().toArray() 方法。
示例 1:
Javascript
moment.js">
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:
Javascript
moment.js">
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/
相關用法
- Moment.js moment().to()用法及代碼示例
- Moment.js moment().toNow()用法及代碼示例
- Moment.js moment().toString()用法及代碼示例
- Moment.js moment().toJSON()用法及代碼示例
- Moment.js moment().toDate()用法及代碼示例
- Moment.js moment().toISOString()用法及代碼示例
- Moment.js moment().toObject()用法及代碼示例
- Moment.js moment().calender()用法及代碼示例
- Moment.js moment().daysInMonth()用法及代碼示例
- Moment.js moment().diff()用法及代碼示例
- Moment.js moment().format()用法及代碼示例
- Moment.js moment().from()用法及代碼示例
- Moment.js moment().fromNow()用法及代碼示例
- Moment.js moment().unix()用法及代碼示例
- Moment.js moment().valueOf()用法及代碼示例
- Moment.js moment().millisecond()用法及代碼示例
- Moment.js moment().set()用法及代碼示例
- Moment.js moment().local()用法及代碼示例
- Moment.js moment().min()用法及代碼示例
- Moment.js moment().get()用法及代碼示例
- Moment.js moment().startOf()用法及代碼示例
- Moment.js moment().isoWeeksInYear()用法及代碼示例
- Moment.js moment().utcOffset()用法及代碼示例
- Moment.js moment().zone()用法及代碼示例
- Moment.js moment().add()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Moment.js moment().toArray() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。