日历星期转换为日期 [英] Convert calendar-week to Date
问题描述
在那里,我可以用它来从calendarweek /年组合得到一个日期实例,一个简单的内置函数?
应该可以进入 10w2005
到一个文本框,我将创建日期 2005年3月7日
从它。
周一应该是第一天,CalendarWeekRule应FirstFullWeek。
我的第一个方法是:
昏暗W¯¯作为的Int32 = 10
昏暗y为= 2005的Int32
昏暗ð作为新的日期(Y,1,1)
D = d.AddDays(7 *宽)
但是,这并不工作,因为FirstDay- CalendarWeekRule不适用。
在此先感谢
修改
这是我已经结束了(感谢fjdumont中的链接)是什么:
公共共享功能FirstDateOfWeek(BYVAL年为整数,BYVAL WEEKOFYEAR为整数)为DATETIME
昏暗jan1作为新的日期时间(年,1,1)
昏暗daysOffset作为整数= CINT(Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) - CINT(jan1.DayOfWeek)
昏暗firstWeekDay为DATETIME = jan1.AddDays(daysOffset)
昏暗curCulture作为System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture
昏暗firstWeek作为整数= curCulture.Calendar.GetWeekOfYear(jan1,curCulture.DateTimeFormat.CalendarWeekRule,curCulture.DateTimeFormat.FirstDayOfWeek)
如果firstWeek< = 1,则
WEEKOFYEAR - = 1
结束如果
返回firstWeekDay.AddDays(WEEKOFYEAR * 7)
端功能
这个线程应该有所帮助。你将不得不解析字符串,有点像这一点。
的String [] SPL = input.ToLower()斯普利特(W)。
INT年= int.Parse(SPL [1]);
INT周= int.Parse(SPL [0]);
is there a simple builtin function that i could use to get a date instance from a calendarweek/year-combination?
It should be possible to enter 10w2005
into a TextBox and i'll create the date 07 March 2005
from it.
Monday should be first day and CalendarWeekRule should be FirstFullWeek.
My first approach was:
Dim w As Int32 = 10
Dim y As Int32 = 2005
Dim d As New Date(y, 1, 1)
d = d.AddDays(7 * w)
But this does not work because the FirstDay- CalendarWeekRule are not applied.
Thanks in advance
Edit:
this is what i've ended with(Thanks to fjdumont for the link):
Public Shared Function FirstDateOfWeek(ByVal year As Integer, ByVal weekOfYear As Integer) As DateTime
Dim jan1 As New DateTime(year, 1, 1)
Dim daysOffset As Integer = CInt(Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) - CInt(jan1.DayOfWeek)
Dim firstWeekDay As DateTime = jan1.AddDays(daysOffset)
Dim curCulture As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture
Dim firstWeek As Integer = curCulture.Calendar.GetWeekOfYear(jan1, curCulture.DateTimeFormat.CalendarWeekRule, curCulture.DateTimeFormat.FirstDayOfWeek)
If firstWeek <= 1 Then
weekOfYear -= 1
End If
Return firstWeekDay.AddDays(weekOfYear * 7)
End Function
This thread should help. You will have to parse the string, somewhat like this.
string[] spl = input.ToLower().Split("w");
int year = int.Parse(spl[1]);
int week = int.Parse(spl[0]);
这篇关于日历星期转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!