當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Convert.ToDateTime方法代碼示例

本文整理匯總了VB.NET中System.Convert.ToDateTime方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Convert.ToDateTime方法的具體用法?VB.NET Convert.ToDateTime怎麽用?VB.NET Convert.ToDateTime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Convert的用法示例。


在下文中一共展示了Convert.ToDateTime方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: ConversionToDateTime

Module ConversionToDateTime
   Public Sub Main()
      Dim dateString As String = Nothing
      
      ' Convert a null string.
      ConvertToDateTime(dateString)
      
      ' Convert an empty string.
      dateString = String.Empty
      ConvertToDateTime(dateString)
      
      ' Convert a non-date string.
      dateString = "not a date"
      ConvertToDateTime(dateString)
      
      ' Try to convert various date strings.
      dateString = "05/01/1996"
      ConvertToDateTime(dateString)
      dateString = "Tue Apr 28, 2009"
      ConvertToDateTime(dateString)
      dateString = "Wed Apr 28, 2009"
      ConvertToDateTime(dateString)
      dateString = "06 July 2008 7:32:47 AM"
      ConvertToDateTime(dateString)
      dateString = "17:32:47.003"
      ConvertToDateTime(dateString)
      ' Convert a string returned by DateTime.ToString("R").
      dateString = "Sat, 10 May 2008 14:32:17 GMT"
      ConvertToDateTime(dateString)
      ' Convert a string returned by DateTime.ToString("o")
      dateString = "2009-05-01T07:54:59.9843750-04:00"
      ConvertToDateTime(dateString)
   End Sub
   
   Private Sub ConvertToDateTime(value As String)
      Dim convertedDate As Date
      Try
         convertedDate = Convert.ToDateTime(value)
         Console.WriteLine("'{0}' converts to {1}.", value, convertedDate)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in the proper format.", value)
      End Try
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:44,代碼來源:Convert.ToDateTime

輸出:

converts to 1/1/0001 12:00:00 AM.
is not in the proper format.
not a date' is not in the proper format.
05/01/1996' converts to 5/1/1996 12:00:00 AM.
Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM.
Wed Apr 28, 2009' is not in the proper format.
06 July 2008 7:32:47 AM' converts to 7/6/2008 7:32:47 AM.
17:32:47.003' converts to 5/30/2008 5:32:47 PM.
Sat, 10 May 2008 14:32:17 GMT' converts to 5/10/2008 7:32:17 AM.
2009-05-01T07:54:59.9843750-04:00' converts to 5/1/2009 4:54:59 AM.

示例2: Example

' 導入命名空間
Imports System.Globalization

Module Example
   Public Sub Main( )
      Console.WriteLine("{0,-18}{1,-12}{2}", "Date String", "Culture", "Result")
      Console.WriteLine()

      Dim cultureNames() As String = { "en-US", "ru-RU","ja-JP" }
      Dim dateStrings() As String = { "01/02/09", "2009/02/03",  "01/2009/03", _
                                      "01/02/2009", "21/02/09", "01/22/09",   _
                                      "01/02/23" }
      ' Iterate each culture name in the array.
      For Each cultureName As String In cultureNames
         Dim culture As CultureInfo = New CultureInfo(cultureName)
        
         ' Parse each date using the designated culture.
         For Each dateStr As String In dateStrings
            Dim dateTimeValue As DateTime
            Try
               dateTimeValue = Convert.ToDateTime(dateStr, culture)
                ' Display the date and time in a fixed format.
                Console.WriteLine("{0,-18}{1,-12}{2:yyyy-MMM-dd}", _
                                  dateStr, cultureName, dateTimeValue)
            Catch e As FormatException 
                Console.WriteLine("{0,-18}{1,-12}{2}", _
                                  dateStr, cultureName, e.GetType().Name)
            End Try            
         Next
         Console.WriteLine()
      Next
   End Sub 
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:33,代碼來源:Convert.ToDateTime

輸出:

Date String       Culture     Result

01/02/09          en-US       2009-Jan-02
2009/02/03        en-US       2009-Feb-03
01/2009/03        en-US       2009-Jan-03
01/02/2009        en-US       2009-Jan-02
21/02/09          en-US       FormatException
01/22/09          en-US       2009-Jan-22
01/02/23          en-US       2023-Jan-02

01/02/09          ru-RU       2009-Feb-01
2009/02/03        ru-RU       2009-Feb-03
01/2009/03        ru-RU       2009-Jan-03
01/02/2009        ru-RU       2009-Feb-01
21/02/09          ru-RU       2009-Feb-21
01/22/09          ru-RU       FormatException
01/02/23          ru-RU       2023-Feb-01

01/02/09          ja-JP       2001-Feb-09
2009/02/03        ja-JP       2009-Feb-03
01/2009/03        ja-JP       2009-Jan-03
01/02/2009        ja-JP       2009-Jan-02
21/02/09          ja-JP       2021-Feb-09
01/22/09          ja-JP       FormatException
01/02/23          ja-JP       2001-Feb-23

示例3: Example

' 導入命名空間
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "hu-HU", "pt-PT" }
      Dim objects() As Object = { 12, 17.2, False, #1/1/2010#, "today", _
                                  New System.Collections.ArrayList(), "c"c, _
                                  "05/10/2009 6:13:18 PM", "September 8, 1899" }
      
      For Each cultureName As String In cultureNames
         Console.WriteLine("{0} culture:", cultureName)
         Dim provider As New CustomProvider(cultureName)
         For Each obj As Object In objects
            Try
               Dim dateValue As Date = Convert.ToDateTime(obj, provider)      
               Console.WriteLine("{0} --> {1}", obj, _
                                 dateValue.ToString(New CultureInfo(cultureName)))
            Catch e As FormatException
               Console.WriteLine("{0} --> Bad Format", obj)
            Catch e As InvalidCastException
               Console.WriteLine("{0} --> Conversion Not Supported", obj)
            End Try
         Next
         Console.WriteLine()
      Next
   End Sub
End Module

Public Class CustomProvider : Implements IFormatProvider
   Private cultureName As String
   
   Public Sub New(cultureName As String)
      Me.cultureName = cultureName
   End Sub
   
   Public Function GetFormat(formatType As Type) As Object _
          Implements IFormatProvider.GetFormat
      If formatType Is GetType(DateTimeFormatInfo) Then
         Console.Write("(CustomProvider retrieved.) ")
         Return New CultureInfo(cultureName).GetFormat(formatType)
      Else
         Return Nothing
      End If   
   End Function
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:46,代碼來源:Convert.ToDateTime

輸出:

en-US culture:
12 --> Conversion Not Supported
17.2 --> Conversion Not Supported
False --> Conversion Not Supported
1/1/2010 12:00:00 AM --> 1/1/2010 12:00:00 AM
(CustomProvider retrieved.) today --> Bad Format
System.Collections.ArrayList --> Conversion Not Supported
c --> Conversion Not Supported
(CustomProvider retrieved.) 05/10/2009 6:13:18 PM --> 5/10/2009 6:13:18 PM
(CustomProvider retrieved.) September 8, 1899 --> 9/8/1899 12:00:00 AM

hu-HU culture:
12 --> Conversion Not Supported
17.2 --> Conversion Not Supported
False --> Conversion Not Supported
1/1/2010 12:00:00 AM --> 2010. 01. 01. 0:00:00
(CustomProvider retrieved.) today --> Bad Format
System.Collections.ArrayList --> Conversion Not Supported
c --> Conversion Not Supported
(CustomProvider retrieved.) 05/10/2009 6:13:18 PM --> 2009. 05. 10. 18:13:18
(CustomProvider retrieved.) September 8, 1899 --> 1899. 09. 08. 0:00:00

pt-PT culture:
12 --> Conversion Not Supported
17.2 --> Conversion Not Supported
False --> Conversion Not Supported
1/1/2010 12:00:00 AM --> 01-01-2010 0:00:00
(CustomProvider retrieved.) today --> Bad Format
System.Collections.ArrayList --> Conversion Not Supported
c --> Conversion Not Supported
(CustomProvider retrieved.) 05/10/2009 6:13:18 PM --> 05-10-2009 18:13:18
(CustomProvider retrieved.) September 8, 1899 --> 08-09-1899 0:00:00

示例4: ConversionToDateTime

Module ConversionToDateTime
   Public Sub Main()
      ' Try converting an integer.
      Dim number As Integer = 16352
      ConvertToDateTime(number)
      
      ' Convert a null.
      Dim obj As Object = Nothing
      ConvertToDateTime(obj)
      
      ' Convert a non-date string.
      Dim nonDateString As String = "monthly"
      ConvertToDateTime(nonDateString)
      
      ' Try to convert various dates.
      Dim dateString As String 
      dateString = "05/01/1996"
      ConvertToDateTime(dateString)
      dateString = "Tue Apr 28, 2009"
      ConvertToDateTime(dateString)
      dateString = "06 July 2008 7:32:47 AM"
      ConvertToDateTime(dateString)
      dateString = "17:32:47.003"
      ConvertToDateTime(dateString)
   End Sub
   
   Private Sub ConvertToDateTime(value As Object)
      Dim convertedDate As Date
      Try
         convertedDate = Convert.ToDateTime(value)
         Console.WriteLine("'{0}' converts to {1}.", value, convertedDate)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in the proper format.", value)
      Catch e As InvalidCastException
         Console.WriteLine("Conversion of the {0} '{1}' is not supported", _
                           value.GetType().Name, value)
      End Try
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:39,代碼來源:Convert.ToDateTime

輸出:

Conversion of the Int32 '16352' is not supported
converts to 1/1/0001 12:00:00 AM.
monthly' is not in the proper format.
05/01/1996' converts to 5/1/1996 12:00:00 AM.
Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM.
06 July 2008 7:32:47 AM' converts to 7/6/2008 7:32:47 AM.
17:32:47.003' converts to 5/28/2008 5:32:47 PM.


注:本文中的System.Convert.ToDateTime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。