0

Good afternoon, I'm new to programming and I'm working with VB.NET.

I need to get the difference between two dates and then list all the intermediate dates in listbox1. I tried the following code but it doesn't work.

Private Sub breaks()

    Dim date1 As Date = DateTimePicker1.Value.ToString("dd/MM/yyyy")
    Dim date2 As Date = DateTimePicker2.Value.ToString("dd/MM/yyyy")

    While date1 <= date2
        Dim result = date1
        ListBox1.Items.Add(result)
        Dim term = 1
        date1 = DateTimePicker1.Value.AddDays(term)

    End While

End Sub

The function is called within a button. When executed it only shows the sidebars but is blank.

The image shows start date 03/10/2020 and end date 03/16/2020, however the result (listbox) does not return anything.

enter image description here

I expected my result to come:
03/10/2020
03/11/2020
03/12/2020
03/14/2020
03/15/2020
03/16/2020

the interval between them. Can anyone tell me what's wrong?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Jose Gomes
  • 35
  • 9

2 Answers2

0

You should avoid using strings for datetimes until they need to be text.

The variable date1 can be used for all the dates, like this:

Dim date1 As Date = DateTimePicker1.Value
Dim date2 As Date = DateTimePicker2.Value

While date1 <= date2
    ListBox1.Items.Add(date1.ToString("MM/dd/yyyy"))
    Dim term = 1
    date1 = date1.AddDays(term)

End While

Also, you should make sure to set Option Strict On as the default for new projects, and set it for the current project.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

You can use some linq for a simple solution

ListBox1.DataSource =
    Enumerable.Range(0, 2 + DateTimePicker2.Value.Subtract(DateTimePicker1.Value).Days).
    Select(Function(offset) DateTimePicker1.Value.AddDays(offset)).
    ToList()

It generates a list of numbers to act as the offset from the initial date, then adds them the specified number of times (different between the dates in days) to create all the dates. No loop required.

Credit to this answer

Edit:

This can also be similarly applied to a DataGridView, but in order to make a single column, we would need to select an anonymous type.

DataGridView1.DataSource =
    Enumerable.Range(0, 2 + DateTimePicker2.Value.Subtract(DateTimePicker1.Value).Days).
    Select(Function(offset) New With {.Date = DateTimePicker1.Value.AddDays(offset)}).
    ToList()
djv
  • 15,168
  • 7
  • 48
  • 72
  • if instead Enumerable.Range "(0, 2)" is set "(0,1)" it also works correcting in the same way that I need it in the simplest way. – Jose Gomes Mar 16 '20 at 19:43
  • @JoseGomes it depends on whether you want to include the end date or not. It also makes it simple to change which way you want to do it. Andrew's answer should work for you too. I personally would do it this way. – djv Mar 16 '20 at 19:44
  • 1
    I do want to include the end date. Thank you for your help – Jose Gomes Mar 16 '20 at 19:52
  • I tried to store the result inside a datagridview with just one column to play the result. Contrary to the loop it didn’t stay as it should, created more columns, etc. Any ideas how can I fix it? I would appreciate – Jose Gomes Mar 16 '20 at 20:03
  • I am using it and I have already marked it as an answer. there's one more problem going on. With the edition you created. I already have a date column created. However he is adding another column date and playing the result inside it. How can I play the range within my existing column? – Jose Gomes Mar 16 '20 at 20:27
  • @JoseGomes it depends on what you want to do. Do you want to append the dates into the existing column? Then you could reserve the list you've already added and concatenate the two lists together. Or if you want a second column with a different name you can rename the property in the anonymous type from .Date to .SomethingElse – djv Mar 16 '20 at 20:31
  • @djv- This, I want to add the dates in my existing column. Could you show me how to do it? I don't intend a second column, I would be grateful – Jose Gomes Mar 16 '20 at 20:52
  • @JoseGomes how did you add the other dates to the DataGridView in the first place? – djv Mar 16 '20 at 20:55
  • Queries are made in the mysql database and then listed in the column ("date"). However these intervals are added before the query in mysql – Jose Gomes Mar 16 '20 at 21:05
  • @JoseGomes think about adding them the same way you added from SQL. Or from SQL use databinding, reserve the List(Of Date) or List(Of String), whichever you used, concatenate it with the new list, and set the DataSource again to the newly concatenated list. We're getting far outside the scope of your original question. You could create a new question with all the details of your current problem. – djv Mar 16 '20 at 21:09
  • Sorry if I don't express myself so well, but I will improve. I just wanted to avoid "New With {.data", because with that he creates a new column and does not throw the value in the existing "date" column, causing column duplication. I tried to follow from what you sent me but without success. future will create a well-expressed question. I appreciate the help. – Jose Gomes Mar 16 '20 at 21:16
  • @JoseGomes the `New With {.Date ...` creates an anonymous type which makes binding a DataGridView easy. It will depend on how you add your data originally. – djv Mar 16 '20 at 21:26