如何将日期转换为yyww&反之亦然VB.NET?

时间:2009-03-20 15:33:07

标签: vb.net

有谁知道如何将任何日期首先转换为星期六,然后再转换为VB.NET中的yyww(weekyear)? 反之亦然,即yyww进入星期六?

2 个答案:

答案 0 :(得分:2)

假设你的星期日开始,你可能会做这样的事情来获得当周的星期六:

Dim daysToAdd As Integer = DayOfWeek.Saturday - yourDate.DayOfWeek
Dim dateSaturday as Date = yourDate.AddDays(daysToAdd)

我没有测试它。 那么你可以通过做类似的事情来获得周年纪念日:

Dim weekYear As Integer = CInt(Math.Ceiling(dateSaturday.DayOfYear / 7))

使用反向逻辑,您可以找到与周年相对应的星期六。

答案 1 :(得分:2)

    Dim d As DateTime = #1/20/2009#
    d = d.AddDays(DayOfWeek.Saturday - d.DayOfWeek)
    Dim myCal As New System.Globalization.GregorianCalendar
    Dim wkOFyr As Integer = myCal.GetWeekOfYear(d, _
                                                Globalization.CalendarWeekRule.FirstDay, _
                                                DayOfWeek.Sunday)
    Dim s As String
    s = d.ToString("yy") & wkOFyr.ToString.PadLeft(2, "0"c)