setDate()在31号设置了错误的日期? [英] setDate() set the wrong date on 31st?

查看:48
本文介绍了setDate()在31号设置了错误的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪,我不知道自己在做什么错。我有一个获取日期的函数(即,这种格式: 06/24/2011 ),这是函数:

This is very weird I don't know what I'm doing wrong. I have a function to grab the date (i.e in this format: 06/24/2011), here's the function:

function checkDate(input){
    var d = new Date();
    var dspl = input.split("/");

    if(dspl.length != 3)
        return NaN;

    d.setDate(dspl[1]);

    d.setMonth(Number(dspl[0])-1);

    if(dspl[2].length == 2)
        d.setYear("20"+(dspl[2]+""));
    else if(dspl[2].length == 4)
        d.setYear(dspl[2]);
    else
        return NaN;

    var dt = jsToMsDate(new Date(d));
    return dt;
}

如果我输入月份中的任何日期,它将正确解析该日期,但是如果我输入31st,即 01/31/2011 ,那么它将变成 01/01/2011 。我不确定该怎么做,也不确定该在哪里。

If I enter any date of the month, it would parse the date correctly, but if I enter 31st, i.e "01/31/2011", then it would turn into "01/01/2011". I'm not sure what to do and not really sure where the problem might be.

推荐答案

JavaScript的 Date 对象允许您给出无效的月份和日期组合;他们会自动为您更正这些错误(例如,如果您将月份为6月,则将月份的日期设置为31,则会自动将其设置为7月1日)。这意味着,如果您单独设置字段,则可能会遇到自动更正的情况。

JavaScript's Date objects allow you to give invalid combinations of months and days; they automagically correct those for you (so for instance, if you set the day of the month to 31 when the month is June, it automatically makes it July 1st). That means if you set the fields individually, you can run into situations where that automagic correction gets in your way.

在您的情况下,如果要全部设置这三个字段中,最好使用 Date 构造函数的形式,该构造函数接受它们作为参数:

In your case, if you're going to set all three of those fields, you're better off using the form of the Date constructor that accepts them as arguments:

var dt = new Date(year, month, day);

(如果需要小时,分钟,秒和毫秒,也可以将它们添加为参数)。

(If you want hours, minutes, seconds, and milliseconds, you can add them as parameters as well.)

因此,请查看您的代码,进行即兴更新:

So looking at your code, an off-the-cuff update:

function checkDate(input){
    var year, month, day, d, dt;
    var dspl = input.split("/");

    if(dspl.length != 3)
        return NaN;

    year  = parseInt(dspl[2], 10);
    month = parseInt(dspl[0], 10) - 1;
    day   = parseInt(dspl[1], 10);
    if (isNaN(year) || isNaN(month) || isNaN(day)) {
        return NaN;
    }

    if (year < 100) {
        year += 2000;
    }

    d = new Date(year, month, day);

    var dt = jsToMsDate(d);
    return dt;
}

有关此更新的其他说明:

Some other notes on that update:


  • 最好使用 parseInt 解析最终用户的数字,并始终指定基数(十进制为10)。 (不, parseInt 的运行速度不低于 Number 或一元 + 技巧。人们以为是,但是它不是。)

  • 无需弄乱字符串即可将2000加到仅用两位数字给出的年份中。但是,如果您愿意,可以。注意我削弱了那里的验证,允许(例如)2001年为一位数字年份,(例如)300 AD为三位数年份。因此,如果需要它那么强大,就需要重新调整它。

  • 无需将日期实例提供给 new Date()

  • It's best to use parseInt to parse numbers from end users, and to always specify the radix (10 for decimal). (No, parseInt is not slower than Number or the unary + trick. People assume it is, but it isn't.)
  • No need to muck about with strings to add 2000 to years given with only two digits. But you can if you like. Note I weakened the validation there, allowing one-digit years for (say) 2001 and three-digit years for (say) 300 AD. So if you need it to be that strong, you'll need to readjust that.
  • No need to feed the date instance into new Date() again.

这篇关于setDate()在31号设置了错误的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆