开发者

Javascript setHours have unexpected behaviour for UTC+2 timezones

开发者 https://www.devze.com 2023-02-16 18:46 出处:网络
I have following javascript function function DateIncrement(_Date,_Inc) { return (new Date((new Date(_Date)).setHours(_Inc*24,0,0,0)))

I have following javascript function

function DateIncrement(_Date,_Inc)
{
    return (new Date((new Date(_Date)).setHours(_Inc*24,0,0,0)))
}

The purpose is to increment _Date by开发者_JS百科 _Inc days. This works fine for all the timezone except UTC+2 time zones (Amman, Cairo, Beirut etc.) For UTC+2 time zones it does not return next day. It set hours in _Date to 24.

Thanks in advance.


If you're trying to increment a JS Date by n days, why don't you use setDate()?

function DateIncrement(_Date,_Inc)
{
    var date = new Date(_Date);
    date.setDate(date.getDate() + _Inc);
    return date;
}

... if you really need to set it to midnight, then use setHours() afterwards.

0

精彩评论

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