将日期和时间加入到 VB.NET 中的 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25578004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Join Date and Time to DateTime in VB.NET
提问by Mark Zukerman
I am retrieving data from DB where there is a separate date and time fields. I want to join them into a DateTime field in my VB.NET project. How would you suggest accomplishing this?
我正在从数据库中检索数据,其中有单独的日期和时间字段。我想将它们加入到我的 VB.NET 项目中的 DateTime 字段中。你会建议如何实现这一点?
I tried this but it's not working for me. "String was not recognized as a valid DateTime."
我试过这个,但它对我不起作用。“字符串未被识别为有效的 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
回答by Guffa
You can just create a new DateTimevalue from the components in InDateand InTime:
您可以DateTime从InDateand 中的组件创建一个新值InTime:
Dim combinedDateTime As DateTime = New DateTime( _
InDate.Year, InDate.Month, InDate.Day, _
InTime.Hour, InTime.Minute, InTime.Second _
)
回答by Matthew
DateTime exposes a method called DateTime.TimeOfDay
DateTime 公开了一个名为DateTime.TimeOfDay的方法
You can simply use DateTime.Addto append the time to the date.
您可以简单地使用DateTime.Add将时间附加到日期。
Dim dateAndTime As DateTime = myDate.Add(myTime.TimeOfDay)
回答by Nathan
Dim CombinedDate As Date = Date1.Date + Time1.TimeOfDay
This will work when you want to combine the DATE from one variable and the TIME from a different variable.
当您想要组合来自一个变量的 DATE 和来自不同变量的 TIME 时,这将起作用。
Date is an alias to DateTime. In VB.NET, they are the same thing. You can see all the aliases in this chart on MSDN. Link
日期是日期时间的别名。在 VB.NET 中,它们是一回事。您可以在 MSDN 上查看此图表中的所有别名。 关联
回答by SKYWALKR
You can do it in one line...declare your indate and use the incident values for the time instead of zeros.
您可以在一行中完成...声明您的日期并使用时间的事件值而不是零。

