我需要生成一个报告,显示52周年(或53周为一些年有)和他们的开始和结束日期。 有一个ISO规格要做到这一点,但似乎非常复杂! IM希望有人知道的方式做到这一点在C#或Visual Basic(其实际上为Visual Basic 6,但我会尝试将它移植跨)
Answer 1:
您可以使用Calendar.GetWeekOfYear
方法来得到一个日期的周数,与CalendarWeekRule.FirstFourDayWeek
值,以指定的几个星期被确定,并且DayOfWeek.Monday
指定的首个工作日。 这是继ISO规范。
例:
int week = Calendar.GetWeekOfYear(DateTime.Today, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
要获得今年的第一周的第一天,你可以从1月份开始第四和回去,直到找到星期一:
DateTime t = new DateTime(DateTime.Today,Year, 1, 4);
while (t.DayOfWeek != DayOfWeek.Monday) t = t.AddDays(-1);
Answer 2:
如果你想要做手工,看看这篇文章 。
Answer 3:
这应该工作。 我用它在过去的报告。 我同意,这是不是很漂亮,但:
DateTime GetWeekStartDate(int year, int week)
{
DateTime jan1 = new DateTime(year, 1, 1);
int day = (int)jan1.DayOfWeek - 1;
int delta = (day < 4 ? -day : 7 - day) + 7 * (week - 1);
return jan1.AddDays(delta);
}
计算某一周的开始日期。 结束日期时间显然是第7天(不含)。
您可能会发现我的这个代码是有用的。 它几乎没有记载,但它实现了对WeekAndYear结构它定义了一些其他的操作。 很多改进的余地。 最值得注意的是,它定义了<
和>
运营商,但没有其他人,这是非常糟糕...但它应该让你开始。
移植到VB6虽然...嗯,也许不会:P
Answer 4:
这些功能捂住了我的要求(适用于ASP经典,从而造成数据类型),希望他们帮助别人太...
Function WeekNumber(dDate)
Dim d2
d2 = DateSerial(Year(dDate - WeekDay(dDate - 1) + 4), 1, 3)
WeekNumber = Int((dDate - d2 + WeekDay(d2) + 5) / 7)
End Function
Function YearStart(iWhichYear)
Dim iWeekDay
Dim iNewYear
iNewYear = DateSerial(iWhichYear, 1, 1)
iWeekDay = (iNewYear - 2) Mod 7
If iWeekDay < 4 Then
YearStart = iNewYear - iWeekDay
Else
YearStart = iNewYear - iWeekDay + 7
End If
End Function
Function WeeksInYear(iYear)
WeeksInYear = WeekNumber(DateAdd("d", -1, YearStart(iYear + 1)))
End Function
Function WeekStart(iYear, iWeek)
WeekStart = DateAdd("ww", iWeek - 1, YearStart(iYear))
End Function
Function WeekEnd(iYear, iWeek)
WeekEnd = DateAdd("d", 6, DateAdd("ww", iWeek - 1, YearStart(iYear)))
End Function
Answer 5:
使用Calendar.GetWeekOfYear方法来得到当前日期时间的星期,其余的应该是微不足道的。
VB6的是没有价值的,你是最好的办法是找到它的辛勤工作给你一个好的图书馆。
Answer 6:
这会给你当前一周的开始
dateAdd(DateAdd(DateInterval.Day, (Now.Day * -1), Now)
为了让周端7天内陆续新增到一周的开始
Answer 7:
System.Globalization.Calendar.GetWeekOfYear
Answer 8:
答案是下面的,是最理解
'1 declaramos las variables Public firstdayweek As Date 'variable para capturar el valor de inicio de semana de una fecha dada Public lastdayweek As Date 'variable para el valor de la fecha final de una semana de una fecha dada Friend Property _NSemana As Integer 'indica el numero de la semana Friend Property _iniciosemana As Date 'contiene la primer fecha de la semana dada 'Fuciones para codigo 'Funcion para calcular la semana actual en la que estamos Function semana() As Date _NSemana = (DateDiff(DateInterval.WeekOfYear, DateTime.Today, New DateTime(DateTime.Today.Year, 1, 1)) *-1) End Function 'esta funcion es la que llamaremos para setear cada valor puedes colocarlo en cualquier evento Public Sub damerangosemana() semana() _iniciosemana = RangoSemana((_NSemana + 1), Today.Year) firstdayweek = _iniciosemana lastdayweek = FinSemana(_iniciosemana) End Sub 'con esta funcion capturamos el dia de la semana y asignamos la fecha incial Public Function RangoSemana(ByVal WeekNumber As Integer, ByVal year1 As Integer) As Date Dim numdia As Integer = 0 Dim oneDate As String Dim PrimerDia As Date oneDate = "1/1/" & year1.ToString PrimerDia = DateAndTime.DateValue(oneDate) 'dayOfYear = inDate.DayOfYear Select Case PrimerDia.DayOfWeek Case DayOfWeek.Sunday numdia = 7 Case DayOfWeek.Monday numdia = 1 Case DayOfWeek.Tuesday numdia = 2 Case DayOfWeek.Wednesday numdia = 3 Case DayOfWeek.Thursday numdia = 4 Case DayOfWeek.Friday numdia = 5 Case DayOfWeek.Saturday numdia = 6 End Select Dim x As Date = DateAdd(DateInterval.Day, 0 - numdia, CType(oneDate, Date)) Dim startdate As Date = DateAdd(DateInterval.WeekOfYear, WeekNumber - 1, x) Return startdate End Function 'funcion para calcular la fecha final Public Function FinSemana(ByVal Date1 As Date) As Date Return DateAdd(DateInterval.Day, 7, Date1) End Function
它'1 declaramos las variables Public firstdayweek As Date 'variable para capturar el valor de inicio de semana de una fecha dada Public lastdayweek As Date 'variable para el valor de la fecha final de una semana de una fecha dada Friend Property _NSemana As Integer 'indica el numero de la semana Friend Property _iniciosemana As Date 'contiene la primer fecha de la semana dada 'Fuciones para codigo 'Funcion para calcular la semana actual en la que estamos Function semana() As Date _NSemana = (DateDiff(DateInterval.WeekOfYear, DateTime.Today, New DateTime(DateTime.Today.Year, 1, 1)) *-1) End Function 'esta funcion es la que llamaremos para setear cada valor puedes colocarlo en cualquier evento Public Sub damerangosemana() semana() _iniciosemana = RangoSemana((_NSemana + 1), Today.Year) firstdayweek = _iniciosemana lastdayweek = FinSemana(_iniciosemana) End Sub 'con esta funcion capturamos el dia de la semana y asignamos la fecha incial Public Function RangoSemana(ByVal WeekNumber As Integer, ByVal year1 As Integer) As Date Dim numdia As Integer = 0 Dim oneDate As String Dim PrimerDia As Date oneDate = "1/1/" & year1.ToString PrimerDia = DateAndTime.DateValue(oneDate) 'dayOfYear = inDate.DayOfYear Select Case PrimerDia.DayOfWeek Case DayOfWeek.Sunday numdia = 7 Case DayOfWeek.Monday numdia = 1 Case DayOfWeek.Tuesday numdia = 2 Case DayOfWeek.Wednesday numdia = 3 Case DayOfWeek.Thursday numdia = 4 Case DayOfWeek.Friday numdia = 5 Case DayOfWeek.Saturday numdia = 6 End Select Dim x As Date = DateAdd(DateInterval.Day, 0 - numdia, CType(oneDate, Date)) Dim startdate As Date = DateAdd(DateInterval.WeekOfYear, WeekNumber - 1, x) Return startdate End Function 'funcion para calcular la fecha final Public Function FinSemana(ByVal Date1 As Date) As Date Return DateAdd(DateInterval.Day, 7, Date1) End Function
文章来源: Calculate the start and end date of a week given the week number and year in C# (based on the ISO specification)