Hi
I have string in YYMMDD format but i need to convert to DD/MMYY format please let me know is there any built in fuctions available in javascript api
ex:130122 to 22/01/13
Please help me
Thanks
Srinivasula Reddy
Printable View
Hi
I have string in YYMMDD format but i need to convert to DD/MMYY format please let me know is there any built in fuctions available in javascript api
ex:130122 to 22/01/13
Please help me
Thanks
Srinivasula Reddy
You can use string.substr() to extract parts of a string. Just cut up your date string and put it together in the order you want.
Possible alternative...
Code:<script type="text/javascript">
String.prototype.toMMDDYY = function(sep) { // expects input format of YYMMDD to convert to MM/DD/YY
return this.substr(2,2)+sep+this.substr(4,2)+sep+this.substr(0,2)
}
// if short year IS KNOWN TO BE in this century
String.prototype.toMMDDYYYY = function(sep) { // expects input format of YYMMDD to convert to MM/DD/YYYY
var tdate = this;
return this.substr(2,2)+sep+this.substr(4,2)+sep+'20'+this.substr(0,2)
}
// whoops ... mis-read original request
String.prototype.toDDMMYY = function(sep) { // expects input format of YYMMDD to convert to DD/MM/YY
return this.substr(4,2)+sep+this.substr(2,2)+sep+this.substr(0,2)
}
// test of above
var str = '130125'; alert(str+' converts to '+str.toMMDDYY('/')+' or '+str.toMMDDYYYY('/')+' or '+str.toDDMMYY('/'));
str = '121225'; alert(str+' converts to '+str.toMMDDYY('-')+' or '+str.toMMDDYYYY('-')+' or '+str.toDDMMYY('-'));
</script>
Or simply, with a regular expression :
;Code:var today="130126";
var todayNewFormat=today.replace/(\d\d)(\d\d)(\d\d)/,'$3/$2/$1');
alert(todayNewFormat);//=>26/01/13
Sorry Srinuetta... Thanks JMRKER !
We must write :
Code:var todayNewFormat=today.replace(/(\d\d)(\d\d)(\d\d)/,'$3/$2/$1');
one simple way:
Code:"yymmdd".split(/(.{2})/).reverse().join("")