如何获取一个月的日历视图的第一天和最后一天(周日至周六) [英] How to get the first and last day of a month calendar view (sunday-saturday)

查看:283
本文介绍了如何获取一个月的日历视图的第一天和最后一天(周日至周六)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日历,该日历的第一个工作日开始于星期日,结束于星期六.

I have a calendar that's first week day starts in Sunday and ends in Saturday.

现在,由于我不知道日历的第一天和最后一天,所以我只能禁用日历月中的几天.

Right now I can only disable days in the calendar current month because I don't know the first and last day in the calendar.

我现在使用的代码非常简单:

The code that Im using is pretty simple right now:

private List<DateTime> GetDisabledDates(DateTime fromDate, DateTime toDate){

// right now fromDate and toDate are the start and end days in a month

var disabledDates = SearchDates(fromDate, toDate);

return disabledDates;

}

因此,考虑到该周从周日开始到周六结束,我需要在日历月中显示第一天和最后一天.

So, what I need is to get the first day and last day showed in the calendar month, considering that week starts in Sunday and ends in Saturday.

关于如何从特定月份中获得第一个和最后一个(带有黄色标记的日期)的线索?考虑日历配置?

Any clue on how to dinamically get first and last (yellow marked dates) from a specific month? Considering the calendar configuration?

推荐答案

最适合我的解决方案:

int totalCalendarDays = 42; // matrix 7 x 6

// set the first month day
DateTime firstDayMonth = new DateTime(date.Year, date.Month, 1);

 // set the lastmonth day
DateTime lastDayMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));

// now get the first day week of the first day month (0-6 Sun-Sat)
byte firstDayWeek = (byte) firstDayMonth.DayOfWeek;

// now get the first day week of the last day month (0-6 Sun-Sat)
byte lastDayWeek = (byte) lastDayMonth.DayOfWeek;

// now the first day show in calendar is the first day month minus the days to 0 (sunday)
DateTime firstDayCalendar = firstDayMonth.Subtract(TimeSpan.FromDays(firstDayWeek));
int tempDays = (lastDayMonth - firstDayCalendar).Days;

DateTime lastDayCalendar = lastDayMonth.Add(TimeSpan.FromDays(totalCalendarDays - tempDays - 1));

也许是一种更好的方法:)

Maybe is a better way to do this :)

这篇关于如何获取一个月的日历视图的第一天和最后一天(周日至周六)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆