从两个日期计算年龄vb.net

问题描述:

我想从日期计算年龄。这可以在服务器计算机上正常工作,但不会在特定客户端报告“字符串07/21/2016无法转换为日期”错误。我发现服务器的语言环境设置为en-US,错误语言环境的客户端设置为en-UK。我尝试了下面的代码,使得年龄计算成为可能,不管系统区域设置如何,但它没有奏效。从两个日期计算年龄vb.net

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)" 
+0

日期从哪里来?用户输入? – christophano

+0

日期来自通过服务器输入的数据库,上面的日期只是为了说明。 – Diamond

+1

我假设(希望?)它们在数据库中存储为'DateTime'?为什么要打扰字符串呢? – christophano

这是我如何简化你的代码,使工作:

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) 

请注意,我替换“/”通过“ - ”为了绕过日期的“斜线问题”(其这里记录:Why can't DateTime.ParseExact() parse "9/1/2009" using "M/d/yyyy")。

另外:我正在简化关于“如何获得数年的时间跨度”的部分(我的简化:只是将它除以365)。如果你想更准确地说,它将需要更多的工作:Format A TimeSpan With Years

+0

谢谢,完美的作品,再次感谢链接。 – Diamond

+0

使用Dim userBirthDate = New DateTime(2010,7,30)'会更简单。 –

我在使用@XavierPena方法时仍然遇到问题,但我在所有情况下使用的全局方法如下;

'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