Moment js:格式日期没有零填充

jho*_*ers 2 javascript format date momentjs

如何使用momentjs格式化日期而不带前导零?例如 2018-6-29 而不是 2018-06-29。

如果数字小于 10,则不应在其前添加 0。

小智 8

根据文档(https://momentjs.com/docs/#/parsing/string-format/),如果您使用MorD代替MMDD在 format() 函数中,您将获得不带 0 的日期。

moment().format("YYYY-M-DD") 是你所追求的。

如果您还想从个位数天数中排除 0,您可以使用:

moment().format("YYYY-M-D")
Run Code Online (Sandbox Code Playgroud)

(在这里小提琴:http : //jsfiddle.net/rLjQx/69671/