将TimeSpan添加到给定的DateTime [英] Adding a TimeSpan to a given DateTime

查看:169
本文介绍了将TimeSpan添加到给定的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想添加1天到$ code> DateTime 。所以我写道:

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());

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

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

有什么问题?

推荐答案

DateTime 值是 immutable 添加方法返回一个新的 DateTime 值与 TimeSpan

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

这个工作原理:

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

这篇关于将TimeSpan添加到给定的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆