Home > Software design >  Convert Dropdownlist String Value to Date
Convert Dropdownlist String Value to Date

Time:11-08

I want to do a date comparison to check whether is the Before Period is bigger than After Periode So far it has been working properly until the date range is a bit tricky

For example The value is from a dropdownlist item Before period is 21-10-2022 After period is 04-11-2022

It will trigger the error message I set if the before period is bigger than the period after

I have a code like this

If CDate(ddlPeriodeBefore.SelectedValue) <= CDate(ddlPeriodeBefore.SelectedValue) Then
   'Does the job if the the before period is smaller than after period
Else
   lblInfo.Text = "Period BEFORE Must Be SMALLER Than Period AFTER."
End If

Can anyone help me? it keeps saying "conversion from string to date is not valid"

I've tried datetime.parse, parse exact, cdate, convert.todatetime but nothing works so far, or maybe I used it the wrong way

Please help, thanks in advance

CodePudding user response:

Tim Schmelter's suggestion is not wrong, but I did want to provide an alternative. Create a collection of Tuple, add the dates (both formatted string and native value) to the collection, then bind the control to the list.

Here is the alternative:

Dim dates As New List(Of Tuple(Of String, DateTime))()
Dim today = DateTime.Today
For daysSubtract = 90 To 0 Step -1
    Dim dateToAdd = today.AddDays(-daysSubtract)
    dates.Add(New Tuple(Of String, DateTime)(dateToAdd.ToString("dd-MM-yyyy"), dateToAdd))
Next

ddlPeriodeBefore.ValueMember = "Item1"
ddlPeriodeBefore.DisplayMember = "Item2"
ddlPeriodeBefore.DataSource = dates

ddlPeriodeAfter.ValueMember = "Item1"
ddlPeriodeAfter.DisplayMember = "Item2"
ddlPeriodeAfter.DataSource = dates

Now when you go to get the selected value, you can use a simpler conversion since the stored object is already a DateTime:

Dim beforePeriod = DirectCast(ddlPeriodeBefore.SelectedValue, DateTime)
Dim afterPeriod = DirectCast(ddlPeriodeAfter.SelectedValue, DateTime)

If (beforePeriod <= afterPeriod) Then
    ' ...
Else
    lblInfo.Text = "Period BEFORE Must Be SMALLER Than Period AFTER."
End If

The advantage to this approach is that you do not have to refactor your code if the date formatting changes.

CodePudding user response:

If DateTime.Parse works depends on your curren culture because you don't pass any. For me this works fine (but for you not):

Dim selectedValue1 = "21-10-2022"
Dim selectedValue2 = "04-11-2022"
Dim beforePeriod = Date.Parse(selectedValue1)
Dim afterPeriod = Date.Parse(selectedValue2)

So either you know the correct culture that you have used when you have created this string, then you can pass this as second parameter to Date.Parse, or you use ParseExact:

Dim beforePeriod = Date.ParseExact(selectedValue1, "dd-MM-yyyy", CultureInfo.InvariantCulture)
Dim afterPeriod = Date.ParseExact(selectedValue2, "dd-MM-yyyy", CultureInfo.InvariantCulture)

The culture is relevant because they can have different date separators and the order of the day-, month- and year-tokens are different.

  • Related