簡體   English   中英

將日期和時間加入到 VB.NET 中的 DateTime

[英]Join Date and Time to DateTime in VB.NET

我正在從數據庫中檢索數據,其中有單獨的日期和時間字段。 我想將它們加入到我的 VB.NET 項目中的 DateTime 字段中。 你會建議如何實現這一點?

我試過這個,但它對我不起作用。 “字符串未被識別為有效的日期時間。”

Dim InDate As New DateTime(incident.IncidentDate.Value.Year, incident.IncidentDate.Value.Month, incident.IncidentDate.Value.Day, 0, 0, 0)
                    Dim InTime As New DateTime(1900, 1, 1, incident.IncidentTime.Value.Hour, incident.IncidentTime.Value.Minute, incident.IncidentTime.Value.Second)


                    Dim combinedDateTime As DateTime = DateTime.Parse((InDate.ToString("dd/MM/yyyy") + " " + InTime.ToString("hh:mm tt")))

                    rdtpIncidentDateTime.SelectedDate = combinedDateTime

你可以創建一個新的DateTime從組件價值InDateInTime

Dim combinedDateTime As DateTime = New DateTime( _
  InDate.Year, InDate.Month, InDate.Day, _
  InTime.Hour, InTime.Minute, InTime.Second _
)

DateTime 公開了一個名為DateTime.TimeOfDay的方法

您可以簡單地使用DateTime.Add將時間附加到日期。

Dim dateAndTime As DateTime = myDate.Add(myTime.TimeOfDay)
Dim CombinedDate As Date = Date1.Date + Time1.TimeOfDay

當您想要組合來自一個變量的 DATE 和來自不同變量的 TIME 時,這將起作用。

日期是日期時間的別名。 在 VB.NET 中,它們是一回事。 您可以在 MSDN 上查看此圖表中的所有別名。 關聯

您可以在一行中完成...聲明您的日期並使用時間的事件值而不是零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM