2012-03-16 34 views
2

我正在尋找一個明確的,標準(即認證爲正確或經過合理徹底測試)計算ISO日期的視覺基本(即在Visual Studio Express的2010 Visual Basic項目運行)在Visual Basic中查找明確的ISO星期計算器(在Visual Studio Express 2010中)

(不成功)的研究至今


+2

你見過這個:http://stackoverflow.com/questions/1226201/calculate-the-start-and-end-date-of-a-week-given-the-week-number-and-year-in -cs – assylias 2012-03-16 17:01:31

+0

+1感謝@assylias一個非常快速的表情表明它可以提供一些線索。我需要更多時間才能深入瞭解。 – therobyouknow 2012-03-16 17:04:50

回答

1

感謝@assylias - 在這個問題的答案是非常接近,如果不正是我在尋找:https://stackoverflow.com/a/1226248/227926

注意要使用此解決方案,我應該補充一點,你需要導入日曆抽象類確定指標:

Imports System.Globalization 

,然後實例日曆的情況下,我選擇了公曆,因爲它是事實上的標準,但也有其他(文化)選項:

Dim myCalendar As Calendar = New GregorianCalendar 

然後,引用該解決方案(https://stackoverflow.com/a/1226248/227926):

可以使用Calendar.GetWeekOfYear方法得到週數 日期,其中CalendarWeekRule.FirstFourDayWeek值指定 周是如何確定的,DayOfWeek.Monday指定第一個工作日的 。這遵循ISO規範。

例子:

int week = myCalendar.GetWeekOfYear(DateTime.Today, 
CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

我不會記住我作爲一個重複的問題,因爲他們的問題的標題「計算給出在C#中的週數和年份一週的開始和結束日期(根據ISO規範)「與我的截然不同,他們提到了C#(不是Visual Basic,雖然他們也想要Visual Basic)。

保持我提出的問題應該可以幫助別人尋找同樣的東西。

謝謝!根據維基百科的文章

+1

上面的答案給出了錯誤的值。考慮日期31/12/2008。這是ISO WEEK號碼1,上面的代碼給出了53的答案。在實踐中,這個解決方案滿足了我的需求,所以這只是對他人的警告。 Wikapedia對有效的53週年有很好的描述。 (http://en.wikipedia.org/wiki/ISO_week_date) – 2012-05-22 10:09:11

+0

+1 @Paul愛爾蘭值得注意。那麼沒有ISO日曆嗎? – therobyouknow 2012-05-23 08:17:20

0

我不知道VS快,但我只是寫了這個(全VS):

Public Function GetIsoYearAndWeek(day As Date) As Integer() 
    Dim g As New System.Globalization.GregorianCalendar() 

    Dim year = g.GetYear(day) 
    Dim dow = g.GetDayOfWeek(day) 

    'Per MSDN, dow is 0 for sunday, per ISO, this should be 7 
    If (dow = 0) Then dow = 7 

    'usually, this calculation gives the week number 
    'http://en.wikipedia.org/wiki/ISO_week_date#Calculation 
    Dim week = Math.Floor((g.GetDayOfYear(day) - dow + 10)/7) 

    'If the week is 0 or 53, we should do some checks 
    If (week = 0) Then 
     'if we get week=0, the week is actually in last year 
     year -= 1 

     'A week has 53 weeks only if it starts on a thursday 
     'or is a leap year starting on a wednesday 
     Dim jan1st = New Date(year, 1, 1, g) 
     If (jan1st.DayOfWeek = DayOfWeek.Thursday _ 
       OrElse jan1st.DayOfWeek = DayOfWeek.Wednesday _ 
         AndAlso g.IsLeapYear(year)) Then 
      week = 53 
     Else 
      week = 52 
     End If 
    ElseIf (week = 53) Then 
     'determine if this week's thursday is in this year, if 
     'it's not, this week is also in the next year 
     Dim thursday = day.AddDays(4 - dow) 
     If (g.GetYear(thursday) > year) Then 
      Return {year + 1, 1} 
     End If 
    End If 

    Return {year, week} 
End Function 
0

[編輯]我只注意到最後一個針對這一主題也由我自己製作,談論重塑車輪。不過,我認爲下面的類對於上面解釋的方法來說是一個很好的包裝,所以我對它進行了修改以包含我在那裏使用的計算方法。

我不知道VSExpress,但在全VB,我只是GregorianCalendar的擴展功能添加到獲得本週一年和簡化訪問正確的週數:

Public Class IsoWeekCalendar 
Inherits Globalization.GregorianCalendar 

'Get the ISO week number 
Public Overloads Function GetWeekOfYear(d As DateTime) As Integer 
    Dim Week As Integer 
    'year is passed byref 
    GetYearAndWeek(d, Week:=Week) 
    Return Week 
End Function 

'Get the year that belongs with the week 
Public Function GetWeekYear(d As DateTime) As Integer 
    Dim Year As Integer 
    'year is passed byref 
    GetYearAndWeek(d, Year:=Year) 
    Return Year 
End Function 

'Get both the week and year by ref 
Public Sub GetYearAndWeek(d As DateTime, Optional ByRef Year As Integer = 0, Optional ByRef Week As Integer = 0) 
    Dim yearweek = GetYearAndWeek(d) 
    Year = yearweek.Item1 
    Week = yearweek.Item2 
End Sub 

'Get both the week and year 
Public Function GetYearAndWeek(d As DateTime) As Tuple(Of Integer, Integer) 
    Dim year = Me.GetYear(d) 
    Dim dow = Me.GetDayOfWeek(d) 

    'Per MSDN, dow is 0 for sunday, per ISO, this should be 7 
    If (dow = 0) Then dow = 7 

    'usually, this calculation gives the week number 
    'http://en.wikipedia.org/wiki/ISO_week_date#Calculation 
    Dim week As Integer = Math.Floor((Me.GetDayOfYear(d) - dow + 10)/7) 

    'If the week is 0 or 53, we should do some checks 
    If (week = 0) Then 
     'if we get week=0, the week is actually in last year 
     year -= 1 

     'A week has 53 weeks only if it starts on a thursday 
     'or is a leap year starting on a wednesday 
     Dim jan1st = New Date(year, 1, 1, Me) 
     If (jan1st.DayOfWeek = DayOfWeek.Thursday _ 
       OrElse jan1st.DayOfWeek = DayOfWeek.Wednesday _ 
         AndAlso Me.IsLeapYear(year)) Then 
      week = 53 
     Else 
      week = 52 
     End If 
    ElseIf (week = 53) Then 
     'determine if this week's thursday is in this year, if 
     'it's not, this week is also in the next year 
     Dim thursday = d.AddDays(4 - dow) 
     If (Me.GetYear(thursday) > year) Then 
      year += 1 
      week = 1 
     End If 
    End If 

    Return Tuple.Create(year, week) 
End Function 

'Get the date from the year, week and DoW, again, using calculation from 
'http://en.wikipedia.org/wiki/ISO_week_date#Calculation 
Public Function GetDate(year As Integer, week As Integer, Optional day As DayOfWeek = DayOfWeek.Monday) As DateTime 
    Dim ordinal = (week * 7) + IsoDayOfWeek(day) - (GetIsoDayOfWeek(New Date(year, 1, 4, Me)) + 3) 
    Return New Date(year, 1, 1, Me).AddDays(ordinal - 1) 
End Function 

'Get the previous date with the supplied DoW 
Public Function GetPrev(d As DateTime, day As DayOfWeek) As DateTime 
    If (d.DayOfWeek <> day) Then 
     Return GetPrev(d.AddDays(-1), day) 
    End If 
    Return d 
End Function 

'Get the next date with the supplied DoW 
Public Function GetNext(d As DateTime, day As DayOfWeek) As DateTime 
    If (d.DayOfWeek <> day) Then 
     Return GetPrev(d.AddDays(1), day) 
    End If 
    Return d 
End Function 

'Get the ISO day of week 
Public Function GetIsoDayOfWeek(time As Date) As Integer 
    Return IsoDayOfWeek(MyBase.GetDayOfWeek(time)) 
End Function 

'Translate .NET DoW to ISO DoW 
Public Function IsoDayOfWeek(d As DayOfWeek) As Integer 
    If (d = DayOfWeek.Sunday) Then Return 7 
    Return d 
End Function 

末級

相關問題