当我将参数传递为星期和年份时如何获取当前日期 [英] How to get the current date when i pass the parameter as week and year
本文介绍了当我将参数传递为星期和年份时如何获取当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将参数传递为星期和年份时,如何获取当前日期.
例子,
我有25-2011(星期和年),如何获取这个星期和年的日期...?
Hi,
How to get the current date when i pass the parameter as week and year.
Example,
I have 25-2011 (week and year), How to get the date for this week and year...?
推荐答案
检查此链接.
http://stackoverflow.com/questions/662379/calculate-date-from-week-number [^ ]
Check this link.
http://stackoverflow.com/questions/662379/calculate-date-from-week-number[^]
此处的逻辑:
1.找出一年中1月1日的星期几(星期日或星期一等).
2.根据步骤1的结果,确定一年中的第一个星期日.
3.如果1月1日本身是星期日,则成为第一周,否则就是第二周.
4.使用第3步中的结果,以一周中有7天这一事实来确定任何特定星期的天数.
Heres the logic:
1. Find out the Day of Week of Jan 1 of the year (Sunday or Monday etc......)
2. Based on results from step 1, determine the first Sunday of the year.
3. If Jan 1st itself is a Sunday, that becomes the first week, else it is the second week.
4. Use results from step 3 to determine the days for any specific week # using the fact that a week has 7 days.
尝试:
private static DateTime GetDateFromWeek(int year, int weekOfYear)
{
DateTime jan1 = new DateTime(year, 1, 1);
int daysOffset = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
int firstWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(jan1, CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
if (firstWeek <= 1)
{
weekOfYear -= 1;
}
return firstMonday.AddDays(weekOfYear * 7);
}
不记得我从哪里得到的,但是你去了...
Can''t remember where I got it, but there you go...
这篇关于当我将参数传递为星期和年份时如何获取当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文