Results 1 to 5 of 5

Thread: [RESOLVED] get days range add

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Resolved [RESOLVED] get days range add

    Hello.
    I can't get this in my mind.
    I want to get the total days in range, today to an x month.
    I know I have to do this:
    (EndDate.Date - StartDate.Date).Days

    The issue is that I am not sure how would I calculate the enddate by the exact date.
    So if I need to add 2 months I would need to calculate the endate , for example 30+31 = 61 not 60.
    I have a difficult day and I really cannot get this in my mind so any help would be appreciated.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: get days range add

    The Date Class has .AddDays(), and .AddMonths(), so there’s no need to calculate the number of days in 2 months

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: get days range add

    I think it depends how you consider your month. In term of time or effort.
    If it is in term of date then it is easy : 12/03 + 2 months = 12/05
    in term of effort, you need to define the number of days in your month (eg : 30 days ) : 12/03 +2 months (2*30 days) =10/05, 12/07 +2 months = 09/09.
    Personally, I would do that in term of time.
    Last edited by Delaney; Nov 24th, 2021 at 04:07 PM. Reason: typo
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: get days range add

    I needed this a while back, see if it helps

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim dc As New System.Globalization.GregorianCalendar()
            Dim formatInfo = System.Globalization.DateTimeFormatInfo.CurrentInfo
            ListBox1.Items.Add("Jahr" & "-" & "Tage " & "-" & "Monat ")
            ListBox1.Items.Add("-----------------------------------------")
            'schreibe in Text datei
            Using sw As New IO.StreamWriter("E:\TestFolder\TestfileYear_Days.txt")
                For i = 2020 To 2023
                    ListBox1.Items.Add("-----------------------------------------")
                    ListBox1.Items.Add(CStr(i) & " - " & dc.GetDaysInYear(i))
                    For y = 1 To 12
                        Dim nDays As New DateTime(i, y, 1)
                        Dim monthName = formatInfo.GetMonthName(y)
                        sw.WriteLine(CStr(i) & " " & dc.GetDaysInMonth(i, y) & " " & monthName)
                        Do While nDays.Month = y
                            sw.WriteLine(nDays.ToString("dd.MM.yyyy"))
                            'ListBox1.Items.Add(nDays.ToString("dd.MM.yyyy"))
                            nDays = nDays.AddDays(1)
    
                        Loop
                        'zeige in Listbox
                        ListBox1.Items.Add(CStr(i) & " - " & dc.GetDaysInMonth(i, y) & " - " & monthName)
                    Next
    
                Next
                sw.Close()
            End Using
    
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: get days range add

    Thanks to all.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width