Given a date how to get Sunday & Saturday of that week

北慕城南 提交于 2019-12-12 20:38:56

问题


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.



来源:https://stackoverflow.com/questions/10516540/given-a-date-how-to-get-sunday-saturday-of-that-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!