• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.Net - 环境设置发布时间:2022-01-22
下一篇:
VB.NET Convert.ChangeType方法代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap