user287745
user287745

Reputation: 3171

Why does the code not add 3hrs to the time?

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

It should add 3 to hrs but does not, it's like the line addhours does not exist.

Upvotes: 0

Views: 69

Answers (1)

Dan Tao
Dan Tao

Reputation: 128307

The DateTime type is immutable.

Luckily, the solution is quite simple:

dt = dt.AddHours(3);

Upvotes: 5

Related Questions