[英].day() returns wrong day of month with Moment.js
我正在使用 Moment.js 解析字符串並分別獲取日、月和年:
var date = moment("12-25-1995", "MM-DD-YYYY");
var day = date.day();
但是, day
不是 25,而是 1。正確的 API 方法是什么?
這是如何獲取日期的一部分:
var date = moment("12-25-1995", "MM-DD-YYYY");
if (date.isValid()) {
day = date.date();
console.log('day ' + day);
month = date.month() + 1;
console.log('month ' + month);
year = date.year();
console.log('year '+ urlDateMoment.year());
} else {
console.log('Date is not valid! ');
}
您可以使用moment().format('DD')
來獲取月份中的日期。
var date = +moment("12-25-1995", "MM-DD-YYYY").format('DD');
// notice the `+` which will convert
// the returned string to a number.
祝你好運...
.day(),獲取從星期一開始輸出值(1-7)的星期幾。
如果那天是
要獲得@David 提到的月份中的某一天,您需要使用 .date() 例如:
const today = moment().date()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.