Javascript Format String -
i have string variable holds date looks this.
<script> var value = "2014-03-24"; </script> i want format string date looks this.
<script> var formatteddata = "03/24/2014"; </script> i need replace "-" "/" , move month/day/year around. can please me? thanks.
try this;
var d = new date("2014-03-24"); d.tolocaledatestring("en-us"); // "3/24/2014" note advantage here can pass through different locales different formats such "en-gb" uk instance give day/month/year. , if want two-digit month;
d.tolocaledatestring("en-us", {day:'2-digit', month:'2-digit', year:'numeric'}); // "03/24/2014" if run browser inconsistencies, can old-school lowest-common-denominator with;
(d.getmonth()+1) + "/" + d.getdate() + "/" + d.getfullyear(); // "3/24/2014" but won't pad day , month 2-digit numbers though. if want that, you'll need use small pad function this;
function paddatez(n) { return n < 10 ? '0' + n : n.tostring(); } (paddatez(d.getmonth()+1)) + "/" + paddatez(d.getdate()) + "/" + d.getfullyear(); // "03/24/2014"
Comments
Post a Comment