将字符串格式转换为日期格式

时间:2019-12-24 12:12:02

标签: vb.net

如何转换此字符串格式

  

03.07.2019

采用以下日期格式?

  

2019-07-03

这是我从Soap Services获得的String格式的样子

enter image description here

Public Class ContractBill
Public StartDate As Date
End Class
Dim ctrBill As New ContractBill With {
    .StartDate = Convert.ToDateTime(BillingData.START_DATE.ToString("yyyy-MM-dd"))
}

这是我尝试将代码转换为所需格式的尝试,但是使用此.StartDate会出现错误:

  

System.InvalidCastException:无法将类型为System.String的对象转换为类型为System.IFormatProvider的对象。

如何获取日期格式yyyy-MM-dd?

2 个答案:

答案 0 :(得分:0)

日期格式没有,因此您必须先将替换为- / ,因此您可以将日期解析为可用格式。

get

VB.net

"a" | "b" | "c" | "d" | "e"

答案 1 :(得分:0)

使用DateTime.TryParseExact方法,因为您知道传入日期的格式:

Dim enUS As New CultureInfo("en-US")
Dim inbound As String = "03.07.2019"
Dim outbound As DateTime
If DateTime.TryParseExact(inbound, "MM.dd.yyyy", enUS, DateTimeStyles.None, outbound) Then
    Console.WriteLine("'{0}' is formated from: {1}", outbound.ToString("yyyy/dd/MM"), inbound)
Else
    Console.WriteLine("'{0}' is not in an acceptable format.", inbound)
End If

提琴:Live Demo

相关问题