如何更改时间日期时间? [英] How to change time in datetime?
问题描述
我如何可以单独更改时间的DateTime
变量?
How can I change the time alone in DateTime
variable?
DateTime s = some datetime;
我需要改变只在时间元件S
。
推荐答案
您不能更改日期时间值 - 这是不可改变的。但是,你可以改变的变量有一个新的价值。这样做,要改变只是时间的最简单的方法是创建有关时间是时间跨度,并使用DateTime.Date属性:
You can't change a DateTime value - it's immutable. However, you can change the variable to have a new value. The easiest way of doing that to change just the time is to create a TimeSpan with the relevant time, and use the DateTime.Date property:
DateTime s = ...;
TimeSpan ts = new TimeSpan(10, 30, 0);
s = s.Date + ts;
取值
现在将是相同的日期,但在10:30。
s
will now be the same date, but at 10.30am.
注意的DateTime
忽略夏令时转换,再presenting天真公历时间在两个方向上(请参阅<一个注释部分href=\"http://msdn.microsoft.com/en-us/library/System.DateTime%28v=vs.110%29.aspx\"><$c$c>DateTime$c$c>文档)。唯一的例外是。现在
和。今天
:他们检索,因为它们发生,反映这些事件的当前系统时间
Note that DateTime
disregards daylight saving time transitions, representing "naive" Gregorian time in both directions (see Remarks section in the DateTime
docs). The only exceptions are .Now
and .Today
: they retrieve current system time which reflects these events as they occur.
这是促使我开始野田佳彦时间的项目,这是目前生产准备的东西的那种。它的 ZonedDateTime
类型是通过其链接到一个提出感知 TZ
数据库条目。
This is the kind of thing which motivated me to start the Noda Time project, which is now production-ready. Its ZonedDateTime
type is made "aware" by linking it to a tz
database entry.
这篇关于如何更改时间日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!