Get start date and end date of current week (week

2020-02-25 04:53发布

I want to display start date and end date of current week. (week start=monday, week end=sunday) I manage to display monday.

But I'm unable to get sunday date. Please post me if anyone has solution.

11条回答
唯我独甜
2楼-- · 2020-02-25 05:43

In the other examples you will have a problem when sunday falls in other month. This should solve the problem:

var today, todayNumber, mondayNumber, sundayNumber, monday, sunday;
    today = new Date();
    todayNumber = today.getDay();
    mondayNumber = 1 - todayNumber;
    sundayNumber = 7 - todayNumber;
    monday = new Date(today.getFullYear(), today.getMonth(), today.getDate()+mondayNumber);
    sunday = new Date(today.getFullYear(), today.getMonth(), today.getDate()+sundayNumber);
查看更多
孤傲高冷的网名
3楼-- · 2020-02-25 05:46

Based on RobG's answer, I ended up with this to get week to start on midnight Monday and end on Sunday 23:59:59.999.

  var weekMap = [6, 0, 1, 2, 3, 4, 5];

  function startAndEndOfWeek(date) {
    var now = new Date(date);
    now.setHours(0, 0, 0, 0);
    var monday = new Date(now);
    monday.setDate(monday.getDate() - weekMap[monday.getDay()]);
    var sunday = new Date(now);
    sunday.setDate(sunday.getDate() - weekMap[sunday.getDay()] + 6);
    sunday.setHours(23, 59, 59, 999);
    return [monday, sunday];
  }
查看更多
Melony?
4楼-- · 2020-02-25 05:50

You can modify the return value to meet your needs, right now it just returns the full object of Sunday.

Date.prototype.endWeek=function(){
  return new Date(this.getFullYear(), this.getMonth(),(7-this.getDay())+this.getDate());
}

//call like this
var myDate = new Date(); 
var Sunday = myDate.endWeek();
alert("Sunday falls on "+ Sunday.getDate());
查看更多
冷血范
5楼-- · 2020-02-25 05:54
<script>
Date.prototype.getWeek = function(start)
{
        //Calcing the starting point
    start = start || 0;
    var today = new Date(this.setHours(0, 0, 0, 0));
    var day = today.getDay() - start;
    var date = today.getDate() - day;

        // Grabbing Start/End Dates
    var StartDate = new Date(today.setDate(date));
    var EndDate = new Date(today.setDate(date + 6));
    return [StartDate, EndDate];
}

// test code
var Dates = new Date().getWeek();
alert(Dates[0].toLocaleDateString() + ' to '+ Dates[1].toLocaleDateString())
</script>

I believe this works.

查看更多
The star\"
6楼-- · 2020-02-25 05:54

The following function will do the trick:

// return an array of date objects for start (monday)
// and end (sunday) of week based on supplied 
// date object or current date
function startAndEndOfWeek(date) {

  // If no date object supplied, use current date
  // Copy date so don't modify supplied date
  var now = date? new Date(date) : new Date();

  // set time to some convenient value
  now.setHours(0,0,0,0);

  // Get the previous Monday
  var monday = new Date(now);
  monday.setDate(monday.getDate() - monday.getDay() + 1);

  // Get next Sunday
  var sunday = new Date(now);
  sunday.setDate(sunday.getDate() - sunday.getDay() + 7);

  // Return array of date objects
  return [monday, sunday];
}

// Mon Nov 12 2012 00:00:00
// Sun Nov 18 2012 00:00:00
alert(startAndEndOfWeek(new Date(2012,10,14)).join('\n'));
查看更多
登录 后发表回答