moment().invalid()方法用於返回無效的 Moment 對象。這可以用於製作我們自己的解析器。該方法接受一個可用於指定給定的對象解析標誌在對象中。我們可以指定任何解析標誌,即使 Moment 無法識別它。
用法:
moment().invalid( Object );
參數:該方法接受單個參數:
- Object: 該對象用於指定該對象的parsingFlags。它是一個可選參數。
返回值:此方法返回無效的 Moment.js 對象。
注意:這在普通的 Node.js 程序中不起作用,因為它需要全局安裝或在項目目錄中安裝外部moment.js 庫。
Moment.js 可以使用以下命令安裝:
moment模塊的安裝:
npm install moment
以下示例將演示 Moment.js moment().invalid() 方法。
示例 1:
Javascript
const moment = require('moment');
let invalidMoment = moment.invalid();
console.log(
"Invalid Moment:", invalidMoment
)
console.log(
"IsValid check:", invalidMoment.isValid()
);
console.log(
"From Now of the Date:", invalidMoment.fromNow()
);
console.log(
"Format of the Date:", invalidMoment.format()
);
輸出:
Invalid Moment: Moment<Invalid date> IsValid check: false From Now of the Date: Invalid date Format of the Date: Invalid date
示例 2:
Javascript
const moment = require('moment');
let invalidMoment2 = moment.invalid(
{
invalidFormat: 'm:h',
invalidMonth: "Decamber",
someInvalidProperty: true
}
);
console.log(
"Invalid Moment:", invalidMoment2
)
console.log(
"IsValid check:", invalidMoment2.isValid()
);
console.log(
"Parsing Flags:", invalidMoment2.parsingFlags()
);
輸出:
Invalid Moment: Moment<Invalid date> IsValid check: false Parsing Flags: { empty: false, unusedTokens: [], unusedInput: [], overflow: -2, charsLeftOver: 0, nullInput: false, invalidEra: null, invalidMonth: 'Decamper', invalidFormat: 'm:h', userInvalidated: false, iso: false, parsedDateParts: [], era: null, meridiem: null, rfc2822: false, weekdayMismatch: false, someInvalidProperty: true }
參考: https://momentjs.com/docs/#/utilities/invalid/
相關用法
- Moment.js moment().inspect()用法及代碼示例
- Moment.js moment().isoWeeksInYear()用法及代碼示例
- Moment.js moment().isoWeek()用法及代碼示例
- Moment.js moment().isoWeekday()用法及代碼示例
- Moment.js moment().isoWeekYear()用法及代碼示例
- 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().to()用法及代碼示例
- Moment.js moment().toNow()用法及代碼示例
- Moment.js moment().unix()用法及代碼示例
- Moment.js moment().valueOf()用法及代碼示例
- Moment.js moment().millisecond()用法及代碼示例
- Moment.js moment().set()用法及代碼示例
- Moment.js moment().toString()用法及代碼示例
- Moment.js moment().local()用法及代碼示例
- Moment.js moment().min()用法及代碼示例
- Moment.js moment().get()用法及代碼示例
- Moment.js moment().startOf()用法及代碼示例
- Moment.js moment().toArray()用法及代碼示例
- Moment.js moment().utcOffset()用法及代碼示例
- Moment.js moment().zone()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Moment.js moment().invalid() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。