Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment js: format date without zero padding

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.

like image 830
jhonathan myers Avatar asked Nov 15 '25 17:11

jhonathan myers


2 Answers

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/)

like image 154
Toby Hamand Avatar answered Nov 18 '25 07:11

Toby Hamand


You should use the format string YYYY-M-D.

Consult the documentation of how to format moments. https://momentjs.com/docs/#/displaying/format/

like image 25
Alexander Lallier Avatar answered Nov 18 '25 08:11

Alexander Lallier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!