为什么代码不会增加3小时?

时间:2010-08-21 07:17:55

标签: c# datetime

    Literal four = new Literal();
    string timeanddate;
    timeanddate = DateTime.UtcNow.ToString();
    DateTime dt = new DateTime();
    dt = Convert.ToDateTime(timeanddate);
    dt.AddHours(3);
    four.Text = "3hr added and this gives>>  " + dt.ToString();
    form1.Controls.Add(four);

它应该添加3到小时但不是,它就像行addhours不存在。

1 个答案:

答案 0 :(得分:5)

The DateTime type is immutable.

幸运的是,解决方案非常简单:

dt = dt.AddHours(3);