vb.net 如何仅设置 DateTime 的时间部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21222648/
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
How to set only time part of a DateTime
提问by Newbie
I need to set datetime variable but it's time part must be 3:00:00 AM.
For example if i call getdate()
now i'll get 1/20/2014 3:52:03 AM
. I need to set it to 1/20/2014 3:00:00 AM
.
Does anybody have a good snippet for that or any ideas how to do it right? would really appreciate some help.
我需要设置 datetime 变量,但它的时间部分必须是 3:00:00 AM。例如,如果我getdate()
现在打电话,我会得到1/20/2014 3:52:03 AM
. 我需要将其设置为1/20/2014 3:00:00 AM
. 有没有人有一个很好的片段或任何想法如何正确地做到这一点?真的很感激一些帮助。
回答by Jamie Dunstan
The constructor for the DateTime allows you to specify hours, minutes and seconds as well as the usual day month and year.
DateTime 的构造函数允许您指定小时、分钟和秒以及通常的日期月份和年份。
Dim now As DateTime = DateTime.Now
Dim myDate = New DateTime(now.Year, now.Month, now.Day, 3, 0, 0, 0)
回答by J...
There are a lot of ways to do this - reading the documentationmight give you more ideas
有很多方法可以做到这一点 - 阅读文档可能会给你更多的想法
Dim someDate As DateTime = DateTime.Now()
Dim threeAmDate As DateTime = New DateTime(someDate.Year, _
someDate.Month, _
someDate.Day, _
3, _ 'hours
0, _ 'minutes
0, _ 'seconds
0) 'milliseconds
Console.WriteLine(threeAmDate)
回答by tgs266
try this
尝试这个
Label1.Text = DateAndTime.Today & " " & TimeOfDay
you can put this in a label or anything and you can change time of day to "3:00:00"
你可以把它放在标签或任何东西中,你可以将一天中的时间更改为“3:00:00”
回答by Fabio
This one is old... But I came across having the same question.
这是旧的......但我遇到了同样的问题。
Dim dt As New DateTime(_DateTo.Ticks + _TimeTo.Ticks)
--> Both '_DateTo' and/or '_TimeTo' could be Timespan or Datetime objects.
--> '_DateTo' 和/或 '_TimeTo' 都可以是 Timespan 或 Datetime 对象。