添加时间跨度给定的日期时间 [英] Adding a TimeSpan to a given DateTime

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

问题描述

我只想为1天添加到的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 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());

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

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