JQuery datepicker - Selecting the last day of the month

心已入冬 提交于 2019-12-11 12:58:52

问题


In a report generator I wrote the user can select a date range to display results for. But since the data for this report is generated monthly the day portion of the date range is really irrelevant. I now have the requirement to change the day of the selected start date to '1' regardless of what the user has selected. I've done this like so:

$('#TextStartDate').datepicker({ maxDate: "+0M", minDate: "-24M", dateFormat: 'm/1/yy' });

The second pard of the requirement is that the day of the selected end date be set to the last day of the month regardless of what day was selected. So if the user selected an end date of 5/16/2011 then the date field would show 5/31/2011. This is where I get stuck.

Any help would be greatly appreciated.


回答1:


The most consistent technique I have found for this is:

  1. Add one month to the select day
  2. Round to the first day of that month
  3. Subtract 1 day.

    Not necessarily pretty, but it works.

Plan B:

  1. Round to first of the selected month
  2. Add 1 month
  3. Subtract 1 day

note: working on a quick sample code will add later



来源:https://stackoverflow.com/questions/5968781/jquery-datepicker-selecting-the-last-day-of-the-month

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