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

时间:2015-07-04 16:40:54

标签: vb.net date datetime datetime-format

我知道如何使用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 in string format

ParseExact Error Two

如果有人能帮我选择正确的日期/时间格式,我将非常感激。

谢谢!

2 个答案:

答案 0 :(得分:0)

我会说:

  

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)) {
   ...
}

答案 1 :(得分:0)

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

尝试:

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

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