Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading zeroes in datestring

Tags:

javascript

I have a date string like the following: 2011-02-03. I want to remove the leading zeroes in the day and month part of the string. How do I do this?

like image 665
Luke Avatar asked Dec 27 '22 07:12

Luke


1 Answers

"2011-02-03".replace(/-0+/g, '-'); // => "2011-2-3"

[Update]

Per @Lucky's question, you can account for other formats that might have a leading zero as such:

"02-03".replace(/(^|-)0+/g, "$1"); // => "2-3"
like image 107
maerics Avatar answered Jan 14 '23 10:01

maerics