0

I am trying to calculate age from dates. This works fine on the server computer but not on a particular client where it report error that "The string 07/21/2016 cannot be converted to a date". I found out that the locale of the server was set to en-US and the client with error locale is set to en-UK. I tried the code below to make the age calculation possible no matter the system locale but it has not worked.

Dim var as string = "07/30/2010"
Dim dob As String = Format(CDate(var & " 01:00:00"), "dd-MM-yyyy hh:mm:ss")
Dim dob1 As Date = DateTime.ParseExact(dob, "dd-MM-yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
Dim todayDate As String = Format(Date.Now, "dd-MM-yyyy hh:mm:ss")
Dim todayDate1 As Date = DateTime.ParseExact(todayDate, "dd-MM-yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
lblDob.Text = var & " (" & DateDiff(DateInterval.Year, dob1, todayDate1) - 1 & " yrs)"
reduckted
  • 2,358
  • 3
  • 28
  • 37
Diamond
  • 608
  • 7
  • 23

3 Answers3

3

This is how I simplified your code and made it work:

    Dim userBirthDateText = "07/30/2010"
    Dim userBirthDate = Date.ParseExact(userBirthDateText.Replace("/", "-"), "MM-dd-yyyy", Nothing)
    Dim currentDate = Date.Now
    Dim age = Math.Floor(currentDate.Subtract(userBirthDate).TotalDays / 365)

Notice that I replace "/" by "-" in order to bypass the "slash problem" in dates (which is documented here: Why can't DateTime.ParseExact() parse "9/1/2009" using "M/d/yyyy" ).

Also: I am simplifying the part about "how to get the timespan in years" (my simplification: just divide it by 365). If you want to make it more exact, it will need more work: Format A TimeSpan With Years .

Community
  • 1
  • 1
Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
0

I still had problems with time when using @XavierPena method but a all round method I used that worked in all cases is as follows;

'date can be in any form and this method worked for either US or UK locale
'this method always solve the problem of date settings e.g 9/24/2016 or 09/24/2016
Dim age as string = "07/30/2010"
'age
    Try
        Dim birthDate = Date.ParseExact(age.replace("/", "-"), "MM-dd-yyyy", Nothing)
        Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")

    Catch ex As Exception
        Try
            Dim birthDate = Date.ParseExact(age.replace("/", "-"), "M-dd-yyyy", Nothing)
            Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")
        Catch ex2 As Exception
            Try
                Dim birthDate = Date.ParseExact(age.replace("/", "-"), "M-d-yyyy", Nothing)
                Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")
            Catch ex3 As Exception
                Try
                    Dim birthDate = Date.ParseExact(age.replace("/", "-"), "MM-d-yyyy", Nothing)
                    Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")
                Catch ex4 As Exception
                    Try
                        Dim birthDate = Date.ParseExact(age.replace("/", "-"), "dd-MM-yyyy", Nothing)
                        Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")
                    Catch ex5 As Exception
                        Try
                            Dim birthDate = Date.ParseExact(age.replace("/", "-"), "dd-M-yyyy", Nothing)
                            Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")
                        Catch ex6 As Exception
                            Try
                                Dim birthDate = Date.ParseExact(age.replace("/", "-"), "d-M-yyyy", Nothing)
                                Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")
                            Catch ex7 As Exception
                                Dim birthDate = Date.ParseExact(age.replace("/", "-"), "d-MM-yyyy", Nothing)
                                Return (Math.Floor(Date.Now.Subtract(birthDate).TotalDays / 365) & " yrs")
                            End Try
                        End Try

                    End Try
                End Try
            End Try
        End Try

    End Try
Diamond
  • 608
  • 7
  • 23
0

I use this code and it works for me.

Dim DoB as DateTime=dateTimePickerDoB.value.toshortdatestring
Dim DtNow as DateTime=Now.ToshortDatestring
Dim Age as Integer=Datediff(DateInterval.Year,DoB,DtNow)
txtage.text=age
Sir-me
  • 1