Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove zeros from Date string

I have a string with the following format: '01/02/2016' and I am trying to get rid of the leading zeros so that at the end I get '1/2/2016' with regex.

Tried '01/02/2016'.replace(/^0|[^\/]0./, ''); so far, but it only gives me 1/02/2016

Any help is appreciated.

like image 613
inside Avatar asked Dec 20 '16 15:12

inside


People also ask

How do you remove zeros from a string?

Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String. To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter. This method replaces the matched value with the given string.

How do I remove 0 from a date in python?

Your answerIf you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d. This only works on Unix (Linux, OS X), not Windows. On Windows, you would use #, e.g. %Y/%#m/%#d.


Video Answer


1 Answers

Replace \b0 with empty string. \b represents the border between a word character and a non-word character. In your case, \b0 will match a leading zero.

var d = '01/02/2016'.replace(/\b0/g, '');
console.log(d); // 1/2/2016

var d = '10/30/2020'.replace(/\b0/g, '');
console.log(d); // 10/30/2020 (stays the same)
like image 94
mbomb007 Avatar answered Sep 30 '22 07:09

mbomb007