该值的正确日期格式是什么?

What is the proper date format for this value?

我知道如何使用 ParseExact,它以字符串形式传递日期加上以字符串形式传递的日期格式。我似乎无法弄清楚约会的正确格式,希望得到一些帮助。

首先是代码:

For Each row In qryDays

   Dim strFloweredDate As String = row.Field(Of String)("FloweredDate")
   MessageBox.Show(strFloweredDate)
   Dim FloweringDate As Date
   FloweringDate = Date.ParseExact(strFloweredDate, "M/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

   ' Also tried the following formats
   ' Dim FloweringDate As Date
   ' FloweringDate = Date.ParseExact(strFloweredDate, "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture)
   ' Dim FloweringDate As Date
   ' FloweringDate = Date.ParseExact(strFloweredDate, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
   ' Dim FloweringDate As Date
   ' FloweringDate = Date.ParseExact(strFloweredDate, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
   ' Dim FloweringDate As DateTime
   ' FloweringDate = DateTime.ParseExact(strFloweredDate, "M/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
   ' Dim FloweringDate As DateTime
   ' FloweringDate = DateTime.ParseExact(strFloweredDate, "M/dd/yyyy h:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
   ' ETC ETC ETC

Next

现在两张照片。 Messagebox 结果之一显示日期,另一个是错误:

如果有人能帮助我选择正确的 date/time 格式,我将不胜感激。

谢谢!

我会说:

M/d/yyyy hh:mm:ss tt

其中 tt 是 AM/PM 指示符。

参见:Custom Date and Time Format Strings

为了确保您正确处理 d/M <-> M/d 问题,您还可以使用 TryParse 并向其传递正确的文化:

DateTime result;
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out result)) {
   ...
}

你没有告诉我们 row 是什么,但鉴于它来自 qryDays,我猜它来自数据库中的记录。可能是 System.Data.DataRow。如果是这样,您的数据很可能 已经 DateTime 类型,因此您不应该通过 String 来使用它。

尝试:

Dim FloweredDate As DateTime = row.Field(Of DateTime)("FloweredDate");

请记住 VB.net 中的 Date 关键字是 System.DateTime 的别名,因此这些类型是等价的。