最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date going to 1 April 2013 instead of 31 March 2013 - Stack Overflow

programmeradmin2浏览0评论

I have these two functions that creates a new string in the correct format (mm-dd-yyyy) but right now it seems to not work so well... when I input the date 31-03-2013 which is a valid date, it es out with 04-01-2013 as in the first of the month after....

Here are the two functions:

Date.prototype.sqlDate = Date.prototype.sqlDate || function () {
    return this.getMonth() + "-" + this.getDate() + "-" + this.getFullYear();
};

String.prototype.sqlDate = String.prototype.sqlDate || function () {
    var date = new Date(0);
    var s = this.split("-");
    //If i log "s" here its output is: 
    //    ["31", "03", "2013", max: function, min: function]
    date.setDate(s[0]);
    date.setMonth(s[1]);
    date.setYear(s[2]);
    return date.sqlDate();
};

I have these two functions that creates a new string in the correct format (mm-dd-yyyy) but right now it seems to not work so well... when I input the date 31-03-2013 which is a valid date, it es out with 04-01-2013 as in the first of the month after....

Here are the two functions:

Date.prototype.sqlDate = Date.prototype.sqlDate || function () {
    return this.getMonth() + "-" + this.getDate() + "-" + this.getFullYear();
};

String.prototype.sqlDate = String.prototype.sqlDate || function () {
    var date = new Date(0);
    var s = this.split("-");
    //If i log "s" here its output is: 
    //    ["31", "03", "2013", max: function, min: function]
    date.setDate(s[0]);
    date.setMonth(s[1]);
    date.setYear(s[2]);
    return date.sqlDate();
};
Share Improve this question edited Mar 12, 2013 at 21:47 FabianCook asked Mar 12, 2013 at 21:15 FabianCookFabianCook 20.6k17 gold badges72 silver badges118 bronze badges 13
  • 2 Remember that JavaScript will return a 0-based month value, whereas date and year will be one-based. So January will be 0. – p.campbell Commented Mar 12, 2013 at 21:17
  • 1 Oh buggers. Forgot this. – FabianCook Commented Mar 12, 2013 at 21:18
  • 1 This "feature" of Javascript is one of the dumbest design decisions I've ever e across. – Pekka Commented Mar 12, 2013 at 21:19
  • Agreed, if they are going to do it... might as well do them all... or none, not just the month -.- – FabianCook Commented Mar 12, 2013 at 21:21
  • 1 @Pekka웃, I wish I could say the same. I've seen even worse, the way the context changes to the window when you call a function, dumbest. – gdoron Commented Mar 12, 2013 at 21:25
 |  Show 8 more ments

2 Answers 2

Reset to default 8

Month of Date is a number between 0-Jan and 11-Dec,

So 3 is April...

It's extremely annoying because:

  • day- 1 to 31. one based index
  • month- 0 to 11. zero based index.

Well... javascript's specs... carry on.

MDN

You can use this to set it right:

date.setMonth(parseInt(s[1], 10) - 1);

You can see it works here :

Try this one:

String.prototype.sqlDate = String.prototype.sqlDate || function () {
    var date = new Date(0);
    var s = this.split("-");
    //If i log "s" here its output is: 
    //    ["31", "03", "2013", max: function, min: function]
    date.setDate(s[0]);
    date.setMonth(parseInt(s[1],10)-1);
    date.setYear(s[2]);
    return date.sqlDate();
};
发布评论

评论列表(0)

  1. 暂无评论