将 TimeSpan 添加到给定的 DateTime

发布于 2024-08-30 08:56:10 字数 324 浏览 5 评论 0原文

我只想向 DateTime 添加 1 天。所以我写道:

 DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
 TimeSpan t = new TimeSpan(1, 0, 0, 0);

 date.Add(t);

 Console.WriteLine("A day after the day: " + date.ToString());

我认为结果将是: 2010 04 30- 10:25:00 但我仍然得到初始日期。

怎么了?

I just want to add 1 day to a DateTime. So I wrote:

 DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
 TimeSpan t = new TimeSpan(1, 0, 0, 0);

 date.Add(t);

 Console.WriteLine("A day after the day: " + date.ToString());

I thought the result would be: 2010 04 30- 10:25:00 but I'm still getting the initial date.

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

幸福不弃 2024-09-06 08:56:10

DateTime不可变Add 方法返回一个新的 DateTime 值与 TimeSpan

这有效:

Console.WriteLine("A day after the day: " + date.Add(t).ToString());

DateTime values are immutable. The Add method returns a new DateTime value with the TimeSpan added.

This works:

Console.WriteLine("A day after the day: " + date.Add(t).ToString());
兲鉂ぱ嘚淚 2024-09-06 08:56:10

您需要更改一行:

date = date.Add(t);

You need to change a line:

date = date.Add(t);
杀お生予夺 2024-09-06 08:56:10

dtb 关于 DateTime 不可变的说法是正确的。可以这样想:DateTime 是一种值类型,这将其与 intdouble 归为同一类别。这些结构的实例不能被修改;它们只能被评估和复制。

考虑这段代码:

int i = 4;

i + 2;     // does not compile, but what if it did?
           // would i become 6? clearly not --
           // i + 2 expresses a NEW value, which can
           // be copied somewhere

i = i + 2; // there we go -- that's better

这类似于:

DateTime d = DateTime.Now;
TimeSpan t = TimeSpan.FromDays(1.0);

d.Add(t);     // compiles (because AddDays is a function),
              // but is really the same as i + 2 above

d = d.Add(t); // that's better

顺便说一句,可能有助于使这一点更清楚的一件事是认识到上面的行 d = d.Add(t)d = d + t。而且您不会在自己的行上编写 d + t,就像您不会在自己的行上编写 i + 2 一样。

dtb is right about DateTime being immutable. Think of it this way: a DateTime is a value type, which puts it in the same category as int or double. Instances of these structures cannot be modified; they can only be evaluated and copied.

Consider this code:

int i = 4;

i + 2;     // does not compile, but what if it did?
           // would i become 6? clearly not --
           // i + 2 expresses a NEW value, which can
           // be copied somewhere

i = i + 2; // there we go -- that's better

This is analogous to:

DateTime d = DateTime.Now;
TimeSpan t = TimeSpan.FromDays(1.0);

d.Add(t);     // compiles (because AddDays is a function),
              // but is really the same as i + 2 above

d = d.Add(t); // that's better

By the way, one thing that might help make this clearer is realizing that the above line, d = d.Add(t), is the same as d = d + t. And you wouldn't write d + t on its own line, just like you wouldn't write i + 2 on its own line.

野の 2024-09-06 08:56:10

DateTime 是不可变的,但 Add 和 Subtract 函数会返回新的 DateTime 供您使用。

DateTime tomorrow = DateTime.Now.AddDays(1);

A DateTime is immutable, but the Add and Subtract functions return new DateTimes for you to use.

DateTime tomorrow = DateTime.Now.AddDays(1);
娜些时光,永不杰束 2024-09-06 08:56:10

仅仅执行 date = date.AddDays(1) 有什么问题吗?

What is wrong with just doing date = date.AddDays(1)?

温馨耳语 2024-09-06 08:56:10

date.Add(t) 的结果就是您想要的:

 DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
 TimeSpan t = new TimeSpan(1, 0, 0, 0);

 // The change is here, setting date to be the *new* date produced by calling Add
 date = date.Add(t);

 Console.WriteLine("A day after the day: " + date.ToString());

The result of date.Add(t) is what you're after:

 DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
 TimeSpan t = new TimeSpan(1, 0, 0, 0);

 // The change is here, setting date to be the *new* date produced by calling Add
 date = date.Add(t);

 Console.WriteLine("A day after the day: " + date.ToString());
二手情话 2024-09-06 08:56:10
date.Add(t);

返回修改后的 DateTime,并且不会更改您调用 Add 方法的原始实例。

date.Add(t);

returns a modified DateTime and does not change the original instance on which you call the Add method on.

夏の忆 2024-09-06 08:56:10

如果 DateTime obj 数据类型是“DateTime?”,DateTime 将不起作用哪个接受空值,在这种情况下 DateTime? dt = DateTime.Now;

        DateTime dateObj = new DateTime();

        dateObj = Convert.ToDateTime(dt.ToString());

        var Month3 = dateObj.AddMonths(3);`

DateTime wont work if DateTime obj datatype is "DateTime?" which takes accepts null values, in such case DateTime? dt = DateTime.Now;

        DateTime dateObj = new DateTime();

        dateObj = Convert.ToDateTime(dt.ToString());

        var Month3 = dateObj.AddMonths(3);`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文