Get the date of next monday, tuesday, etc

前端 未结 10 1106
轻奢々
轻奢々 2020-11-29 02:24

I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn\'t come this week yet, I would like the date to be this week, else, next week. Thank

相关标签:
10条回答
  • 2020-11-29 03:09

    if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.

    var date=new Date();
    getNextDayOfWeek(date, 2);
    

    // this is for finding next tuesday

    function getNextDayOfWeek(date, dayOfWeek) {
    // Code to check that date and dayOfWeek are valid left as an exercise ;)
    
    var resultDate = new Date(date.getTime());
    
    resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
    
    return resultDate;
    }
    

    Hope this will be helpfull to you, thank you

    0 讨论(0)
  • 2020-11-29 03:12

    PHP 7.1:

    $next_date = new DateTime('next Thursday');
    $stamp = $next_date->getTimestamp();
    

    PHP manual getTimestamp()

    0 讨论(0)
  • 2020-11-29 03:24

    If I understand you correctly, you want the dates of the next 7 days?

    You could do the following:

    for ($i = 0; $i < 7; $i++)  
      echo date('d/m/y', time() + 86400 * $i);
    

    Check the documentation for the date function for the format you want it in.

    0 讨论(0)
  • 2020-11-29 03:25

    You can use Carbon library.

    Example: Next week friday

    Carbon::parse("friday next week");
    
    0 讨论(0)
提交回复
热议问题