Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从两个日期计算年龄vb.net_Vb.net_Date_Datetime - Fatal编程技术网

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

从两个日期计算年龄vb.net,vb.net,date,datetime,Vb.net,Date,Datetime,我试图从日期计算年龄。这在服务器计算机上可以正常工作,但在报告“字符串07/21/2016无法转换为日期”错误的特定客户端上则不行。我发现服务器的语言环境设置为en-US,而带有错误语言环境的客户端设置为en-UK。我尝试了下面的代码,使年龄计算成为可能,无论系统的地区,但它没有工作 Dim var as string = "07/30/2010" Dim dob As String = Format(CDate(var & " 01:00:00"), "dd-MM-yyyy hh:mm

我试图从日期计算年龄。这在服务器计算机上可以正常工作,但在报告“字符串
07/21/2016
无法转换为日期”错误的特定客户端上则不行。我发现服务器的语言环境设置为
en-US
,而带有错误语言环境的客户端设置为
en-UK
。我尝试了下面的代码,使年龄计算成为可能,无论系统的地区,但它没有工作

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

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

    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)
请注意,我将“/”替换为“-”,以绕过日期中的“斜杠问题”(这里有文档记录:)


另外:我正在简化关于“如何以年计算时间跨度”的部分(我的简化:只需将其除以365)。如果要使其更精确,则需要做更多的工作:。

这就是我简化代码并使其工作的方式:

    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)
请注意,我将“/”替换为“-”,以绕过日期中的“斜杠问题”(这里有文档记录:)


另外:我正在简化关于“如何以年计算时间跨度”的部分(我的简化:只需将其除以365)。如果你想让它更精确,它将需要更多的工作:。

我在使用@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

在使用@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

日期从哪里来?用户输入?日期来自通过服务器输入的数据库,上面的日期只是为了说明。我假设(希望?)它们存储为数据库中的
DateTime
?为什么还要处理字符串呢?如果它们在数据库中存储为DateTime,为什么还要处理字符串呢?当然,它们可以,而且您使用parse exact的方法是正确的,您只需要知道格式。这个问题仍然有效——为什么还要管字符串呢?日期从哪里来?用户输入?日期来自通过服务器输入的数据库,上面的日期只是为了说明。我假设(希望?)它们存储为数据库中的
DateTime
?为什么还要处理字符串呢?如果它们在数据库中存储为DateTime,为什么还要处理字符串呢?当然,它们可以,而且您使用parse exact的方法是正确的,您只需要知道格式。这个问题仍然有效-为什么还要麻烦字符串呢?谢谢,非常好用,再次感谢链接。使用
Dim userBirthDate=New DateTime(2010,7,30)
会更简单。谢谢,非常好用,再次感谢链接。使用
Dim userBirthDate=New DateTime(2010,7,30)
会更简单。