-1

For my project, I need to convert year, week and day of week in DateTime. For this, I use this link : https://github.com/dotnet/runtime/blob/b41f1c5f2fde25d752d857a54c3af24145060cdd/src/libraries/System.Private.CoreLib/src/System/Globalization/ISOWeek.cs#L102-L139

BUT, in my current case, this solution can't function, for example : 1989-01-01 and 1989-12-31.

This two dates start and finish the last day of week and belong to the 52nd week.

Have you an idea ?

Thanks you :)

vek
  • 11
  • 4
  • Why does it not work for you? – Fildor Jan 09 '23 at 13:57
  • related: https://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date – Drag and Drop Jan 09 '23 at 14:02
  • @Fildor : if I have as information "week = 52, year = 1989, day of week = 7 (sunday)", how I can differenciate 1989-01-01 and 1989-12-31 ? – vek Jan 09 '23 at 14:04
  • you can use this blogpost data to hilight your issue : https://learn.microsoft.com/fr-fr/archive/blogs/shawnste/iso-8601-week-of-year-format-in-microsoft-net. but .net core ISOWeek.GetYear and ISOWeek.GetWeekOfYear are correct – Drag and Drop Jan 09 '23 at 14:05
  • @Drag and Drop : I check this. I am in .Net Framework 4.7.2 – vek Jan 09 '23 at 14:06
  • 1
    But in "weeks"-count, 1989-01-01 is Week 52, **1988** , right? (And I should know. I was there ;D ) – Fildor Jan 09 '23 at 14:10
  • @DragandDrop Did you intentionally link to the french article? => english: https://learn.microsoft.com/en-US/archive/blogs/shawnste/iso-8601-week-of-year-format-in-microsoft-net – Fildor Jan 09 '23 at 14:11
  • 1
    @Fildor : oh s..., yes is week 52 of 1988 >_< I see how I can implement this. – vek Jan 09 '23 at 14:14
  • @DragandDrop Laisse tomber. I guessed it must have been something like that :) – Fildor Jan 09 '23 at 14:16
  • @vek, what your input? dayofweek : 0, week :52, year: 1988 ; works. Are you starting from a date, converting to a dayofweek /week /year then back to date? – Drag and Drop Jan 09 '23 at 14:23
  • @DragandDrop : for my unit test, yes. I create a DateTime, convert to my object "DateWeek" (dayofweek / week / year) and re-convert to DateTime. Now, it's OK – vek Jan 09 '23 at 15:12
  • I would like to indicate which comments have helped me but I don't have a button for it. My reputation score is not high enough for that? – vek Jan 09 '23 at 15:21
  • Not sure if it 100% lines up with your requirements but perhaps look at the ISOWeek class https://learn.microsoft.com/en-us/dotnet/api/system.globalization.isoweek?view=net-7.0 – Hursey Jan 10 '23 at 00:49
  • @Hursey: not compatible, I use .Net Framework 4.7.2. Is only for .Net Standard or .Net Core and .Net 5+ – vek Jan 10 '23 at 08:55

1 Answers1

0

Solution :

''' <summary>
''' Convert the <see cref="DateTime"/> object passed as a parameter to the <see cref="DateWeek"/> object (according to ISO 8601)
''' </summary>
''' <param name="value"></param>
''' <returns></returns>
''' <remarks>https://stackoverflow.com/a/1497606/15423682</remarks>
Public Shared Function FromDateTime(value As DateTime) As DateWeek
    ' https://github.com/dotnet/runtime/blob/b41f1c5f2fde25d752d857a54c3af24145060cdd/src/libraries/System.Private.CoreLib/src/System/Globalization/ISOWeek.cs
    ' in current country (FR), first day of week is Monday
    Dim _dayOfWeek As Integer = GetWeekday(value.DayOfWeek)
    If (_dayOfWeek = 0) Then
        _dayOfWeek = 7 ' in .Net, Sunday is 0
    End If

    ' Get the week number for the current value
    Dim week As Integer = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                                    value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday
                                    )
    Dim year As Integer = value.Year
    ' if the current value is during week 52 and only for first month
    If (week >= 52) Then
        ' In ISO 8601, the first Thurday is in first week.
        ' If the current value is before Thurday, is in previous week

        ' The last week of the year (52 or 53) is the one containing the last Thursday of the year.
        ' It is the last week to have the majority of its days (at least 4) in the year and systematically contains
        ' 28 December. It is also the one whose Sunday is closest to 31 December.

        ' It ends no earlier than 28 December or no later than 3 January (so the 4th day belongs to the first week)
        ' https://fr.wikipedia.org/wiki/ISO_8601#Syst%C3%A8me_de_num%C3%A9rotation
        If (value.Month = 1 And value.Day < 4) Then
            year -= 1
        End If
    End If

    Return New DateWeek(_dayOfWeek, week, year)
End Function
vek
  • 11
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '23 at 02:16