javascript - get next day of a string

后端 未结 8 1267
一向
一向 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条回答
  • 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));
    }
    
    0 讨论(0)
  • 2020-12-16 04:27

    You can do the following:

    var nextDay;
    var example = "05-10-1983";
    
    nextDay = new Date(example);
    nextDay.setDate(nextDay.getDate() + 1);
    

    #getDate/#setDate gets/sets the current day of the month (1-31).

    After the above is run, nextDay will be set to whatever tomorrow's date is. This will also rollover to the next month / year if it's the end of the month, and even handle leap years. :)

    0 讨论(0)
  • 2020-12-16 04:29

    So

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

    doesn't work because of daylight saving barriers. I ran into the same problem. I ended up doing something like this:

    function next_day(date) {
      var e = new Date(date.getTime() + 24 * 60 * 60 * 1000);
      if e.getHours() != date.getHours() {
        e = new Date(e.getTime() + (e.getHours() - date.getHours()) * 60 * 60 * 1000)
      }
      return e;
    }
    
    0 讨论(0)
  • 2020-12-16 04:30

    There are leap seconds, leap days, DST, etc., so this can be a tricky problem to solve in all cases.

    In my opinion, the best way to address this (without a date library) is to take advantage of the Date constructor overflow feature[1]:

    main();
    
    function main() {
      var date = uniqueDateParse( '05-10-1983' );
      var newDate = nextDay( date );
    
      print( date );
      print( newDate );
    }
    
    function uniqueDateParse( string ) {
      var stringArray = string.split( '-', 3 );
      var month = stringArray[ 0 ],
          day   = stringArray[ 1 ],
          year  = stringArray[ 2 ];
      // Per ISO 8601[2], using Pacific Daylight Time[3].
      var dateString = year + '-' + month + '-' + day + 'T00:00:00-07:00';
      return new Date( dateString );
    } 
    
    function nextDay( date ) {
      return new Date( date.getFullYear()
                     , date.getMonth()
                     , date.getDate() + 1 );
    }
    
    function print( object ) {
      console.log( object );
    }
    

    Links

    [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters

    [2] http://www.w3.org/TR/NOTE-datetime

    [3] http://www.timeanddate.com/time/zones/pdt

    0 讨论(0)
  • 2020-12-16 04:36

    This would do it for simple scenarios like the one you have:

    var example = '05-10-1983';
    var date = new Date();
    var parts = example.split('-');
    date.setFullYear(parts[2], parts[0]-1, parts[1]); // year, month (0-based), day
    date.setTime(date.getTime() + 86400000);
    alert(date);
    

    Essentially, we create an empty Date object and set the year, month, and date with the setFullYear() function. We then grab the timestamp from that date using getTime() and add 1 day (86400000 milliseconds) to it and set it back to the date using the setTime() function.

    If you need something more complicated than this, like support for different formats and stuff like that, you should take a look at the datejs library which does quite a bit of work for you.

    0 讨论(0)
  • 2020-12-16 04:39

    You can find out day index by getDay() function and create an array of days strings in following manner-

    day = new Date(YourDate);
    
    var dayArray = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    
    day = dayArray[day.getDay()+1];
    
    0 讨论(0)
提交回复
热议问题