2012-03-16 5 views
2

Je suis à la recherche d'un standard définitif (c'est-à-dire certifié correct ou testé raisonnablement) pour calculer la semaine ISO à partir d'une date, en Visual de base (par exemple à exécuter dans un projet Visual studio express 2010 Visual Basic)Je cherche une calculatrice de semaine ISO définitive en Visual Basic (dans Visual Studio Express 2010)

(Unsuccessful) recherche jusqu'à présent:


+2

Avez-vous vu ceci: http://stackoverflow.com/questions/1226201/calculate-the-start-and-end-date-of-a-week-given-the-week-number-and-year-in -cs – assylias

+0

+1 Merci @assylias un regard très rapide suggère qu'il pourrait fournir quelques indices. J'aurai besoin de plus de temps pour regarder plus en profondeur. – therobyouknow

Répondre

1

Merci @assylias - une réponse à cette question est assez proche, sinon exactement ce que je cherche: https://stackoverflow.com/a/1226248/227926

Remarque Pour utiliser cette solution, je dois ajouter que vous devez importer le calendrier classe abstraite défintion:

Imports System.Globalization 

ET instanciez une instance de calendrier, j'ai choisi le calendrier grégorien est la norme de fait, mais il y a d'autres (options culturelles):

Dim myCalendar As Calendar = New GregorianCalendar 

Ensuite, pour citer la solution (https://stackoverflow.com/a/1226248/227926):

Vous pouvez utiliser la méthode Calendar.GetWeekOfYear pour obtenir le numéro de la semaine d'une date, avec la valeur CalendarWeekRule.FirstFourDayWeek pour spécifier comment les semaines sont déterminées, et DayOfWeek.Monday pour spécifier le premier jour de la semaine . Cela suit la spécification ISO.

Exemple:

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

Je ne vais pas marquer ma question comme un doublon, parce que leur titre de la question « Calculer la date de début et la fin d'une semaine étant donné le numéro de la semaine et l'année en C# (basé sur la spécification ISO) "est distinctement différente de la mienne et ils mentionnent C# (pas Visual Basic bien qu'ils veuillent aussi Visual Basic).

Garder ma question présente devrait aider les autres dans leur recherche de la même chose.

Merci!

+1

La réponse ci-dessus donne la mauvaise valeur. Considérez la date 31/12/2008. Ceci est la semaine ISO numéro 1, le code ci-dessus donne une réponse de 53. En pratique, cette solution répond à mes besoins, ce n'est qu'un avertissement pour les autres. Wikapedia a une bonne description des années valables de 53 semaines. (http://en.wikipedia.org/wiki/ISO_week_date) –

+0

+1 @Paul Ireland mérite d'être noté. N'y a-t-il pas un calendrier ISO? – therobyouknow

0

Je ne sais pas VS Express, mais je viens d'écrire ceci (en pleine VS) en fonction de l'article wikipedia: Je viens de remarquer

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

[EDIT] la dernière réponse à ce fil a également été fait par moi-même, parle de réinventer la roue. Cependant, je pense que la classe suivante est un bon wrapper pour la méthode expliquée ci-dessus, donc je l'ai modifié pour incorporer la méthode de calcul que j'ai utilisée ici.

Je ne suis pas sûr de VSExpress, mais en VB plein, je venais de prolonger GregorianCalendar pour ajouter des fonctionnalités pour obtenir l'année la semaine et de simplifier l'accès au numéro de la semaine correcte:

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 

End Class

Questions connexes