本文介绍了给定星期数之间的天数名称和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只是找不到解决我问题的方法.
我有一个星期数,例如13,我也想在这个星期和几天之间约会...
输出为:
第13周(2012):
3月25日,星期日
3月26日,星期一
3月27日,星期二
3月28日,星期三
3月29日,星期四
3月30日,星期五
3月31日,星期六
Just can''t find a solution for my problem..
I have a weeknumber, e.g. 13 and I want date in between this week and days also...
the output to be:
Week 13 (2012):
25 March Sunday
26 March Monday
27 March Tuesday
28 March Wednesday
29 March Thursday
30 March Friday
31 March Saturday
推荐答案
// ----------------------------------------------------------------------
public void DaysOfWeekSample()
{
Week week = new Week( 2012, 13 );
foreach ( Day day in week.GetDays() )
{
Console.WriteLine( day );
}
} // DaysOfWeekSample
该课程还支持ISO 8601周编号:
The class supports also the ISO 8601 week numbering:
// ----------------------------------------------------------------------
public void DaysOfWeekSample()
{
TimeCalendar calendar = new TimeCalendar(
new TimeCalendarConfig { YearWeekType = YearWeekType.Iso8601 } );
Week week = new Week( 2012, 13, calendar );
foreach ( Day day in week.GetDays() )
{
Console.WriteLine( day );
}
} // DaysOfWeekSample
int searchYear = 2012;
int searchWeek = 13;
DateTime searchStartDate = new DateTime(searchYear, 1, 1) - TimeSpan.FromDays(7);
Calendar calendar = CultureInfo.CurrentCulture.Calendar;
int weekOfYear = 0;
do
{
searchStartDate += TimeSpan.FromDays(7);
weekOfYear = calendar.GetWeekOfYear(searchStartDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
} while (weekOfYear != searchWeek);
// now searchStartDate is in the week you are looking for
此解决方案的缺点是您必须遍历所有星期.
请注意定义星期的规则.
Disadvantage of this solution is, that you have to loop through all the weeks.
Pay attention on the rules defining the week.
const int weekNumber = 9;
const int daysNumber = weekNumber * 7;
var dtNow = DateTime.Today;
var firstDateInYear = new DateTime(dtNow.Year, 1, 1);
var lastDayInWeek = firstDateInYear.AddDays(daysNumber);
var firstDayInWeek = lastDayInWeek.AddDays(-7);
for (var day = firstDayInWeek; day <= lastDayInWeek; day = day.AddDays(1))
{
Console.WriteLine(day.Day + " " + day.Month + " " + day.DayOfWeek);
}
这篇关于给定星期数之间的天数名称和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!