var str ="20110725";
var dd = str.substring(6);
var mm = str.substring(4,2);
var yyyy = str.substring(0,3);
alert(dd );//25
alert(mm);//11
alert(yyyy );//2011
Instead of the above output, I expected "25" as date, "07" as month 开发者_如何转开发and "2011" as year. Please correct me.
I think you want substr()
, not substring()
. They're different.
You should have
var mm = str.substring(4, 6);
Instead of:
var mm = str.substring(4, 2)
Do:
var mm = str.substring(4, 6)
Try this. You also need a 4, not a 3 for your year.
var str ="20110725";
var dd = str.substr(6);
var mm = str.substr(4,2);
var yyyy = str.substr(0,4);
alert(dd );//25
alert(mm);//11
alert(yyyy );//2011
精彩评论