开发者

Why is the substring method behaving in this manner?

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜