通过传递星期数和年份来获取星期的第一个和最后一个日期 [英] get first and last date of week by passing week number and year
问题描述
嗨..
我如何获得星期的开始和结束日期.
我会通过..周号和年份...
或任何日期
并返回该周的开始和结束日期...
例子
周数= 1
年= 2012
1/1/2012 8/1/2012
第48周
年= 2011
2012年1月2日2012年6月1日
我尝试输入此代码
hi ..
how i get start and end date of week..
i will pass .. Week number and year ...
or Any Date
and it return start and end date of that week...
example
week number = 1
year = 2012
1/1/2012 8/1/2012
week number 48
year = 2011
1/2/2012 6/1/2012
I TRY THIS CODE
DateTime strtdt = DateTime.ParseExact(tx_dt.Text, "dd/MM/yyyy", null);
GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
int week = cal.GetWeekOfYear(strtdt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
int year = strtdt.Year;
DayOfWeek day = DayOfWeek.Monday;
DateTime startOfYear = new DateTime(year, 1, 1);
// The +7 and %7 stuff is to avoid negative numbers etc.
int daysToFirstCorrectDay = (((int)day - (int)startOfYear.DayOfWeek) + 7) % 7;
DateTime FirstDay = startOfYear.AddDays(7 * (week - 1) + daysToFirstCorrectDay);
DateTime Endaday = startOfYear.AddDays(7 * (week - 1) + daysToFirstCorrectDay).AddDays(6);
TextBox4.Text = FirstDay.ToString("dd/MM/yyyy");
TextBox5.Text = Endaday.ToString("dd/MM/yyyy");
但是它只对年份有效...当年份超过53周时...
它的工作正常的2011年,2012年..但是当我检查2013年时,它给所有下星期date
but it working fine only for year .. .when previoues year week is 53...
its working fine for 2011 , 2012 .. but when i m checking for 2013 it giving next week date
推荐答案
thnks ...我只是错了. ..
我可以简单地实现
由此,
thnks for all ... i just going to wrong side ...
i can simply achive that
by this,,,
DateTime Firstday = dt.AddDays(-(int)dt.DayOfWeek);
DateTime Endaday = Firstday.AddDays(6);
关注此链接,
http://stackoverflow.com/questions/662379/calculate-date-from-week-number
Follow this link,
http://stackoverflow.com/questions/662379/calculate-date-from-week-number
2013年从星期二开始.因此,当您采用第一个星期一作为第一周时,您的代码将返回第二周.
根据您的编码逻辑,我了解您将第一个星期一作为第一个星期计算.
因此它将返回日期的第二个星期.
Hi,
The year 2013 starts from Tuesday. So when you take the first week by taking first Monday, your code returns the second week.
As per you code logic, I understood you calculated first Monday as first week.
So it returns the the second week of dates.
这篇关于通过传递星期数和年份来获取星期的第一个和最后一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!