2016-03-27 288 views
-1

我想從日期計算年齡。這可以在服務器計算機上正常工作,但不會在特定客戶端報告「字符串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

回答

1

這是我如何簡化你的代碼,使工作:

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)'會更簡單。 –

0

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