Javascript 使用momentjs中的链接获取当前月份失败
为什么我不能像这样把瞬间约会对象链起来?说这个月是我可以期待我得到5,但我得到的格式错误不是一个函数 这应该行得通Javascript 使用momentjs中的链接获取当前月份失败,javascript,momentjs,Javascript,Momentjs,为什么我不能像这样把瞬间约会对象链起来?说这个月是我可以期待我得到5,但我得到的格式错误不是一个函数 这应该行得通 console.log(moment().month().format('M')) 月份();方法用于获取/设置月份。如果使用moment().month(),则给出当前月份。如果使用moment().month(3),则设置月份 moment().format('MMM'); //Output: May moment().format('MM'); //Output: 05
console.log(moment().month().format('M'))
月份();方法用于获取/设置月份。如果使用moment().month()
,则给出当前月份。如果使用moment().month(3)
,则设置月份
moment().format('MMM'); //Output: May
moment().format('MM'); //Output: 05
执行moment().month()
moment将返回当前月份的编号(索引从0开始)。但是,当您使用moment().month(3)
设置月份,然后使用.format()
函数时,它将起作用,因为moment().month()
返回一个矩对象,因此具有format()
方法
NOTE: While setting month be aware that index starts from 0.
i.e. If you do moment().month(3).format('MMM'); You will get "Apr" and not "Mar"
这应该起作用,因为矩()
返回一个对象(矩对象),该对象具有方法格式
moment().format('M');//should work
moment().month()
可能返回一个数字,该数字没有方法format()
,因此显示错误。这就像做5.format('M')
你是说moment().format('M')
moment().month().format('M');//will evaluate to 5.format()