Given a date how to get Sunday & Saturday of that

2019-07-19 11:24发布

问题:

I want to get the Sunday & Saturday of the week from which a date is provided. I have access to the following functions only:

  • getDate() returns a number from 0-6 (0 being sunday)
  • getDay() returns a number from 1-31
  • getMonth() returns a number from 0-11
  • getFullYear() returns the current year

I am doing this on titanium.

回答1:

Per your description above, I came up with:

var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));

If I follow the MDN doc, I come up with (works in Ti too):

var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDay() + input.getDate());
var sun = new Date(input.getFullYear(), input.getMonth(), input.getDate() + (input.getDay() - 6));

Where input is a javascript Date object.

The date object will take care or changing the month and/or year if necessary.

Hope this helps.