vb.net 在VB.net中用字符串创建新日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25321530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create New Date with string in VB.net
提问by user1761176
I have string with format:
我有格式的字符串:
Dim date As String = "2014/08/20 21:00"
I can't create new date with this format string
我无法使用此格式字符串创建新日期
Dim date2 As DateTime =DateTime.Parse(date ,CultureInfo.InvariantCulture)
Can convert string "2014/08/20 21:00" to "20/08/2014 9:00 PM" ?
可以将字符串 "2014/08/20 21:00" 转换为 "20/08/2014 9:00 PM" 吗?
回答by HengChin
As MarkHall suggested, do avoid using any keyword as an identifier.
正如 MarkHall 建议的那样,请避免使用任何关键字作为标识符。
and if your dateString is always in the format of yyyy/MM/dd HH:mm
并且如果您的 dateString 始终采用以下格式 yyyy/MM/dd HH:mm
Try below:
试试下面:
Dim dateString As String = "2014/08/20 03:00"
Dim formattedDate As String = DateTime.ParseExact(dateString, "yyyy/MM/dd HH:mm", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy h:mm tt")
回答by Waelhi
try this:
尝试这个:
Dim date As DateTime = "2014/08/20 21:00"
Dim date2 as Datetime = format(date,"yyyy/MM/dd hh:mm tt")
回答by Winston Madiano
Specify the format on the ToString parameter
在 ToString 参数上指定格式
Dim dtStr As String = Convert.ToDateTime("2014/08/20 21:00").ToString("dd/MM/yyyy h:mm tt")
'Output
'20/08/2014 9:00 PM

