Is there a way to replace a portion of a String at a given position in java script. For instance I want to replace 00
in the hours column with 12
in the below string.The substring
comes at 13 to 15.
Mar 16, 2010 00:00 AM
String are immutable in Java. You can't change them. You need to create a new string with the character replaced.
The following is one option:
var myString = "Mar 16, 2010 00:00 AM";
myString = myString.substring(0, 13) +
"12" +
myString.substring(15, myString.length);
Note that if you are going to use this to manipulate dates, it would be recommended to use some date manipulation methods instead, such as those in DateJS.
A regex approach
"Mar 16, 2010 00:00 AM".replace(/(.{13}).{2}/,"$112")
Mar 16, 2010 12:00 AM
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With