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

Calculate age from two dates vb.net

我正试着根据日期来计算年龄。这在服务器计算机上运行良好,但在报告"字符串07/21/2016无法转换为日期"错误的特定客户机上不起作用。我发现服务器的locale设置为en-US,有错误locale的客户机设置为en-UK。我尝试了下面的代码,以使年龄计算成为可能,无论系统区域设置,但它没有工作。

1
2
3
4
5
6
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)"


这就是我如何简化您的代码并使其工作的方法:

1
2
3
4
    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)

请注意,我将"/"替换为"-"以绕过日期中的"斜线问题"(这里记录了:为什么不能使用"m/d/yyyy"来分析"9/1/2009")。

另外:我正在简化关于"如何以年为单位获得时间跨度"的部分(我的简化:只需除以365)。如果你想让它更精确,它将需要更多的工作:格式化一个时间跨度与年。


在使用@xavierpena方法时,我仍然有时间方面的问题,但是我使用的一个适用于所有情况的全面方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'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