How to get first and last day of the week in JavaS

2019-01-03 05:11发布

I have today = new Date(); object. I need to get first and last day of the current week. I need both variants for Sunday and Monday as a start and end day of the week. I am little bit confuse now with a code. Can your help me?

16条回答
倾城 Initia
2楼-- · 2019-01-03 05:42

You can also use following lines of code to get first and last date of the week:

var curr = new Date;
var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
var lastday = new Date(curr.setDate(curr.getDate() - curr.getDay()+6));

Hope it will be useful..

查看更多
走好不送
3楼-- · 2019-01-03 05:42

The excellent (and immutable) date-fns library handles this most concisely:

const start = startOfWeek(date);
const end = endOfWeek(date);
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-03 05:42

SetDate will sets the day of the month. Using setDate during start and end of the month,will result in wrong week

var curr = new Date("08-Jul-2014"); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first)); // 06-Jul-2014
var lastday = new Date(curr.setDate(last)); //12-Jul-2014

If u setting Date is 01-Jul-2014, it will show firstday as 29-Jun-2014 and lastday as 05-Jun-2014 instead of 05-Jul-2014

So overcome this issue i used

var curr = new Date();
day = curr.getDay();
firstday = new Date(curr.getTime() - 60*60*24* day*1000); //will return firstday (ie sunday) of the week
lastday = new Date(curr.getTime() + 60 * 60 *24 * 6 * 1000); //adding (60*60*6*24*1000) means adding six days to the firstday which results in lastday (saturday) of the week
查看更多
倾城 Initia
5楼-- · 2019-01-03 05:43

I recommend to use Moment.js for such cases. I had scenarios where I had to check current date time, this week, this month and this quarters date time. Above an answer helped me so I thought to share rest of the functions as well.

Simply to get current date time in specific format

        case 'Today':
        moment().format("DD/MM/YYYY h:mm A");

        case 'This Week':
          moment().endOf('isoweek').format("DD/MM/YYYY h:mm A");

Week starts from Sunday and ends on Saturday if we simply use 'week' as parameter for endOf function but to get Sunday as the end of the week we need to use 'isoweek'.

        case 'This Month':
          moment().endOf('month').format("DD/MM/YYYY h:mm A");

        case 'This Quarter':
          moment().endOf('quarter').format("DD/MM/YYYY h:mm A");

I chose this format as per my need. You can change the format according to your requirement.

查看更多
贼婆χ
6楼-- · 2019-01-03 05:44

The moment approach worked for me for all the cases ( although i have not test the boundaries like year end , leap years ). Only Correction in the above code is the parameter is "isoWeek" , if you want to start the week from Monday.

    let startOfWeek = moment().startOf("isoWeek").toDate();
    let endOfWeek = moment().endOf("isoWeek").toDate();
查看更多
Explosion°爆炸
7楼-- · 2019-01-03 05:45

Be careful with the accepted answer, it does not set the time to 00:00:00 and 23:59:59, so you can have problems.

I recommend using Moment.js to deal with dates. For your case:

var startOfWeek = moment().startOf('week').toDate();
var endOfWeek   = moment().endOf('week').toDate();

This is just a small use case, it's really simple to do a lot of complex operations.

You can see learn more here: http://momentjs.com/

查看更多
登录 后发表回答