Is there a way to replace a portion of a String at a given position in javascript.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
Is there a way to replace a portion of a String at a given position in javascript.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
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.
One option would be
>>> var test = "Mar 16, 2010 00:00 AM";
>>> test.replace(test.substring(13,15),"12")
if it is always 00:
in hours,
you can just replace 00:
with 12:
using replace()
,
if not u need find the indexOf
the :
character ,
and then replace 2 digit before with 12
.
A regex approach
"Mar 16, 2010 00:00 AM".replace(/(.{13}).{2}/,"$112")
Mar 16, 2010 12:00 AM