Moment js: format date without zero padding
12,264
Solution 1
According to the docs (https://momentjs.com/docs/#/parsing/string-format/) if you use M
or D
instead of MM
or DD
in the format() function you will get the date without the 0.
moment().format("YYYY-M-DD")
is what you are after.
if you also want to exclude the 0 from single digit days you can use:
moment().format("YYYY-M-D")
(fiddle here: http://jsfiddle.net/rLjQx/69671/)
Solution 2
You should use the format string YYYY-M-D
.
Consult the documentation of how to format moments. https://momentjs.com/docs/#/displaying/format/
Related videos on Youtube
Author by
jhonathan myers
Updated on June 04, 2022Comments
-
jhonathan myers almost 2 years
How can I format a date with momentjs without leading zeros? E.g. 2018-6-29 instead of 2018-06-29.
If the number is smaller than 10 it should not add 0 before it.
-
jhonathan myers almost 6 yearsthanks, i tried that but got error, but other thing caused it