javascript - get next day of a string

后端 未结 8 1284
一向
一向 2020-12-16 04:22

I\'ve a var example = \"05-10-1983\"

How I can get the \"next day\" of the string example?

I\'ve try to use Date object...b

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 04:26

    The problem with the +86400000 approach is the potential for error when crossing a daylight savings time barrier. For example, I'm on EST. If I do this:

    var d = new Date("11/04/2012 00:00:00");
    var e = new Date(d.getTime() + 86400000);
    

    e is going to be 11/4/2012 23:00:00 If you then extract just the date portion, you get the wrong value. I recently hit upon this issue while writing a calendar control.

    this will do it better (and with a flexible offset which will let you do more than 1 day in the future):

    function getTomorrow(d,offset) {
        if (!offset) { offset = 1 }
        return new Date(new Date().setDate(d.getDate() + offset));
    }
    

提交回复
热议问题