How is the week number reset to 1 when the the first year is over, or for consecutive years

Gary Simpson 471 Reputation points
2022-01-20T12:16:20.07+00:00

Hi Good People,

How do I reset the week number to ( 1 ) when the year is over.
My start Date would be whatever the user chooses Lets say they chosen ( 01 01 2019, (dd MM yyyy) (week number ( 1 )

The Code I have for the first year is ...

(The date is selected by a Date Time Picker then the value selected, is put in a Textbox called ( TxtDateFinancialYearStarts )

 Dim startDate As DateTime = GetDateFromString(TxtDateFinancialYearStarts.Text)
 Dim newDate As DateTime = GetDateFromString(TxtDate.Text)
 Dim dateDif As TimeSpan = newDate.Subtract(startDate)
 Dim weekNo As Integer = (dateDif.Days / 7) + 1
 LBWeekNumber.Text = weekNo.ToString()

The first Year is OK.

'Then I Get Last Day of the Financial Year

        Dim lastdayOfYear As DateTime = DtpDate.Value.AddYears(1).AddDays(-1)
        TxtLastDayofFinancialYear.Text = lastdayOfYear.ToString("dd  MM  yyyy")

The last Day of the year also get put into another Textbox called ( TxtLastDayofFinancialYear.Text )

Here is where I want to check if the date has surpassed the Last day of the year ( 31 12 2019, ( dd MM yyyy )

Then here is where I want to reset the Week number to ( 1 ) if the Last day of the first year has been Surpassed. also for more consecutive years.

Can Someone please Help

Kind Regards
Gary

Sorry I forgot to add Code for getting date from string.

Function GetDateFromString(s As String) As Date
        Dim d As Date
        If Date.TryParse(s, d) Then Return d
        Return Now
    End Function
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
{count} votes

Accepted answer
  1. LesHay 7,136 Reputation points
    2022-02-11T17:21:37.793+00:00

    Hi
    Maybe ............
    Have a look at this to see if it helps. This example seems to deal with the task you are asking about.

    I have not shown the changing of the FirstDayOfWeek, but if needed then it is trivial to test those variables out too.

    The example uses a DataGridView named 'dgv' as shown below. I have set some test dates surrounding a leap year, but they can be editted to suit for testing.

    173629-111.png

    'https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.dateandtime.datepart?view=netframework-4.7.2&f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(Microsoft.VisualBasic.DateAndTime.DatePart);k(TargetFrameworkMoniker-.NETFramework,Version%253Dv4.7.2);k(DevLang-VB)%26rd%3Dtrue  
      
    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        With dgv  
          .Rows.Add("01 Jan 2023")  
          .Rows.Add("31 Dec 2023")  
          .Rows.Add("01 Jan 2024")  
          .Rows.Add("31 Dec 2024")  
          .Rows.Add("01 Jan 2025")  
          .Rows.Add("31 Dec 2025")  
        End With  
        Calc()  
      End Sub  
      Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValidated  
        If e.ColumnIndex = 0 Then Calc()  
      End Sub  
      Sub Calc()  
        For i As Integer = 0 To dgv.RowCount - 1  
          If Not i = dgv.NewRowIndex Then  
            dgv(1, i).Value = DatePart(DateInterval.WeekOfYear, CDate(dgv(0, i).Value), FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1)  
            dgv(2, i).Value = DatePart(DateInterval.WeekOfYear, CDate(dgv(0, i).Value), FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFullWeek)  
            dgv(3, i).Value = DatePart(DateInterval.WeekOfYear, CDate(dgv(0, i).Value), FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFourDays)  
      
          End If  
        Next  
      End Sub  
    End Class  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2022-01-20T15:18:59.457+00:00

    @Gary Simpson , I think Calendar.GetWeekofYear is what you looking for
    system.globalization.calendar.getweekofyear

    Here same page sample in VB

    Public Sub SampleGetWeek()  
        ' Gets the Calendar instance associated with a CultureInfo.  
        Dim myCI As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")  
        Dim myCal As System.Globalization.Calendar = myCI.Calendar  
      
        ' Gets the DTFI properties required by GetWeekOfYear.  
        Dim myCWR As System.Globalization.CalendarWeekRule = myCI.DateTimeFormat.CalendarWeekRule  
        Dim myFirstDOW As DayOfWeek = myCI.DateTimeFormat.FirstDayOfWeek  
      
        ' Displays the number of the current week relative to the beginning of the year.  
        Console.WriteLine("The CalendarWeekRule used for the en-US culture is {0}.", myCWR)  
        Console.WriteLine("The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW)  
        Console.WriteLine("Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear(DateTime.Now, myCWR, myFirstDOW))  
      
        ' Displays the total number of weeks in the current year.  
        Dim LastDay As DateTime = New System.DateTime(DateTime.Now.Year, 12, 31)  
        Console.WriteLine("There are {0} weeks in the current year ({1}).", myCal.GetWeekOfYear(LastDay, myCWR, myFirstDOW), LastDay.Year)  
      End Sub  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Gary Simpson 471 Reputation points
    2022-02-11T12:14:21.257+00:00

    Hi JoseZero-8614.
    Firstly may I say sorry for the very late reply.

    Secondly I have had trouble with my Computer I had to re-install windows and I have lost everything including databases Programs I had written.
    So I am starting afresh with Visual Basic.

    Thirdly Thank you for getting back to me on this question.

    With the code you kindly posted Does not help me. as I am using Date time picker, textboxes, Labels.

    The procedure I am trying to use is....

    1. When a user selects a date from a DateTimePicker, The selected date is then inserted into a textbox called (TxtDateFinancialYearStarts)

    2) A second DateTimePicker is now use, and when a date selection is made, A Label called (LbWeekNo) gives the current week number.

    But When the date has passed one year (Last day of the First Year), I want the Label (LbWeekNo) to reset to week (1) as if a new year has started.
    I know some years have 52 weeks and Some years have 53 Weeks (in Financial Years)

    The Code I have right now is below...

    Private Sub DtpDateFinancialYearStarts_ValueChanged(sender As Object, e As EventArgs) Handles DtpDateFinancialYearStarts_ValueChanged
            TxtDateFinancialYearStarts.Text = GetDateFromString(DtpDateFinancialYearStarts.Text)
            GetWeekNumber()
    

    End Sub

    Private Sub GetWeekNumber()

        Dim startDate As Date = GetDateFromString(DtpDateFinancialYearStarts.Text)
        'Dim Day As FirstDayOfWeek = TxtFirstDayOfTheWeek.Text
        Dim newDate As DateTime = Date.Now()
        Dim dateDif As TimeSpan = newDate.Subtract(start Date)
        Dim weekNo As Integer = (dateDif.Days \ 7) + 1
        TxtWeekNumber.Text = weekNo.ToString()
    
        Dim weekNumber As Integer
        If WeekNumber < 10 Then
            TxtWeekNumber.Text = "0" & weekNo.ToString()
        Else
            TxtWeekNumber.Text = weekNo.ToString()
        End If
    
    End Sub
    
    
    
    Function GetDateFromString(s As String) As Date
            Dim d As Date
            If Date.TryParse(s, d) Then Return d
            Return Now
        End Function
    

    Kind Regards
    Gary

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.