moment().toObject() 方法用於將 Moment 對象作為 JavaScript 對象返回,並在屬性中包含日期參數。
用法:
moment().toObject();
參數:該方法不接受任何參數:
返回值:此方法以 JavaScript 對象的形式返回持續時間。
注意:這在普通的 Node.js 程序中不起作用,因為它需要全局安裝或在項目目錄中安裝外部moment.js 庫。
Moment.js 可以使用以下命令安裝:
moment模塊的安裝:
npm install moment
以下示例將演示 Moment.js moment().toObject() 方法。
示例 1:
Javascript
const moment = require('moment');
let momentOne = moment();
let momentTwo = moment()
.add(15, 'months')
.add(10, 'days')
.add(24, 'seconds');
console.log(
"Object form of momentOne is:",
momentOne.toObject()
)
console.log(
"Object form of momentTwo is:",
momentTwo.toObject()
)輸出:
Object form of momentOne is: {
years: 2022,
months: 6,
date: 10,
hours: 23,
minutes: 36,
seconds: 3,
milliseconds: 659
}
Object form of momentTwo is: {
years: 2023,
months: 9,
date: 20,
hours: 23,
minutes: 36,
seconds: 27,
milliseconds: 659
}
示例 2:
Javascript
const moment = require('moment');
let momentA = moment(
'25/12/2022', 'DD/MM/YYYY'
);
let momentB = moment({
year: 2017, month: 5, day: 4,
hour: 1, minute: 15, second: 30,
millisecond: 100
});
console.log(
"Object form of momentA is:",
momentA.toObject()
)
console.log(
"Object form of momentB is:",
momentB.toObject()
)輸出:
Object form of momentA is: {
years: 2022,
months: 11,
date: 25,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0
}
Object form of momentB is: {
years: 2017,
months: 5,
date: 4,
hours: 1,
minutes: 15,
seconds: 30,
milliseconds: 100
}
參考: https://momentjs.com/docs/#/displaying/as-object/
相關用法
- Moment.js moment().to()用法及代碼示例
- Moment.js moment().toNow()用法及代碼示例
- Moment.js moment().toString()用法及代碼示例
- Moment.js moment().toArray()用法及代碼示例
- Moment.js moment().toJSON()用法及代碼示例
- Moment.js moment().toDate()用法及代碼示例
- Moment.js moment().toISOString()用法及代碼示例
- 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().toObject() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
