In C#, I want to show the first week in a calendar (in a html table) and I am trying to figure out the most elegant algorithm to generate the right days.
If the first day of the week is not Sunday, I want to show the days of preceding month (like you would see on a regular calendar). So, as an input you have a current month. In this case May. I want to generate this:
Month: May
<table>
<tr>
<th>S</th>
<th>M</th>
<th>T</th>
<th>W</th>
<th>TH</th>
<th>F</th>
<th>Sa</th>
</tr>
<tr>
<td>25</td>
<td>26</td>
<td>27</td>
<td>28</td>
<td>29</td>
<td>30</td>
<td>1</td>
</tr></table>
so it should display something like this (开发者_开发问答ignore the alignment)
S | M | T | W | Th | F | Sa |
25 - 26 - 27 - 28 - 29 - 30 - 1given that each month would have the start of day be a different day of the week, I am trying to figure out an elegant way to get the values of this data using the DateTime object. I see it has a dayofweek property on a date.
I am generating this table in C# on my server to pass down to a html page.
You can use the following code to get the first week:
public static IEnumerable<DateTime> GetFirstWeek(int year, int month) {
DateTime firstDay = new DateTime(year, month, 1);
firstDay = firstDay.AddDays(-(int) firstDay.DayOfWeek);
for (int i = 0; i < 7; ++i)
yield return firstDay.AddDays(i);
}
public static IEnumerable<DateTime> GetFirstWeek(int month, int year)
{
var firstDay = new DateTime(year, month, 1);
var dayOfWeek = firstDay.DayOfWeek;
var firstWeekDay = firstDay.AddDays(-(int)dayOfWeek);
for (int i = 0; i < 7; i++)
{
yield return firstWeekDay.AddDays(i);
}
}
This should work:
DateTime date = new DateTime(year, month, 1);
DayOfWeek firstDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
int daysBack = (7 + date.DayOfWeek - firstDay) % 7;
Enumerable.Range(-daysBack, 7)
.Select(x => date.AddDays(x))
.ToList()
.ForEach(d =>
{
// a place for html generation
});
Here's one simple way to do this.
//Code not tested thoroughly.
private DateTime[] GetWeek(int month)
{
DateTime firstDayofMonth = new DateTime(DateTime.Now.Year, month, 1);
if (firstDayofMonth.DayOfWeek == DayOfWeek.Sunday)
return GetWeek(firstDayofMonth);
else
{
DateTime sundayOfPreviousMonth = firstDayofMonth;
do
{
sundayOfPreviousMonth = sundayOfPreviousMonth.AddDays(-1);
} while (sundayOfPreviousMonth.DayOfWeek != DayOfWeek.Sunday);
return GetWeek(sundayOfPreviousMonth);
}
}
private DateTime[] GetWeek(DateTime date)
{
if (date.DayOfWeek != DayOfWeek.Sunday)
throw new ArgumentException("Invalid weekday.");
DateTime[] week = new DateTime[7];
for (int i = 0; i < week.Length; i++)
{
week[i] = date.AddDays(i);
}
return week;
}
Check this code:
var days = new[]
{
"S", "M", "T", "W", "TH", "F", "Sa"
};
var date = DateTime.Parse( "01.05.2010" );
var dayOfWeek = (int)date.DayOfWeek;
for( var i = 0; i < 7; i++ )
{
Console.WriteLine( days[i] + ": " + date.AddDays( i - dayOfWeek ) );
}
This is just code to give you an idea - for instance should you use a CultureInfo to parse your date. I use any date as an input right now, but you can cut it down to the first day of each month, too.
精彩评论