访问格式化日期字符串

时间:2011-10-17 03:57:24

标签: vb.net ms-access

我在旧的尘土飞扬的vb.net应用程序中有一个where子句

Dim dsAutoFoods As New DataSet
Dim tblAutoFoods As DataTable
Dim adAutoFoods As New OleDbDataAdapter("[qryFilter AutoFoods for Disagged Primary Foods]", connection )
adAutoFoods.SelectCommand.CommandType = CommandType.StoredProcedure
adAutoFoods.Fill(dsAutoFoods, "AutoFoods")
tblAutoFoods = dsAutoFoods.Tables(0)

sb = "INTAKEDATE = Format('18/09/2011 12:00:00 AM','DD/MM/YYYY HH:NN:SS AM/PM')"

Dim rootFood As DataRow() = tblAutoFoods.Select(sb)

我不知道如何格式化where子句以正确使用日期字符串'18 / 09/2011 12:00:00 AM'。

如何将字符串格式化为日期?

我用过:

Dim dateValue As Date
Date.TryParseExact(rdrKeySet("IntakeDate").ToString(), _
   "dd/MM/yyyy hh:mm:ss tt", _
   System.Globalization.CultureInfo.InvariantCulture, _
   Globalization.DateTimeStyles.None, dateValue)

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用Date的ParseExact或TryParseExact方法来解析字符串到日期,

Dim value As Date
Date.TryParseExact("17/10/2011", "dd/MM/yyyy", 
      System.Globalization.CultureInfo.InvariantCulture, 
           Globalization.DateTimeStyles.None, value)

sb = "INTAKEDATE=#" value & "#"

'or 

sb = "INTAKEDATE='" value & "'"