Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why DateTime.AddHours doesn't seem to work?

Tags:

I have same result 1338161400 when I do

    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    DateTime date = DateTime.Parse(@"28/05/12 01:30");
    TimeSpan diff = date.ToUniversalTime() - origin;
    Console.WriteLine( (Math.Floor(diff.TotalSeconds)).ToString());

as well as when I use date.AddHours(-4) :

    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    DateTime date = DateTime.Parse(@"28/05/12 01:30");
    date.AddHours(-4);
    TimeSpan diff = date.ToUniversalTime() - origin;
    Console.WriteLine( (Math.Floor(diff.TotalSeconds)).ToString());

I try to get 1338168600 like http://www.mbari.org/staff/rich/utccalc.htm

Update:

Thanks I changed to

    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    DateTime date = DateTime.Parse(@"28/05/2012 01:30");
    date = date.AddHours(-4);
    date = date.ToUniversalTime();
    TimeSpan diff = date - origin;
    Console.WriteLine((Math.Floor(diff.TotalSeconds)).ToString());  

But I got 1338147000 still not 1338168600

like image 452
user310291 Avatar asked May 28 '12 08:05

user310291


People also ask

How accurate is DateTime now in C#?

It tends to be between 0.5 and 15 milliseconds.

Is DateTime now static?

DateTime. Today is static readonly . So supposedly it should never change once (statically) instantiated.

Does DateTime now use local time?

The Now property returns a DateTime value that represents the current date and time on the local computer.

What is the use of addhours in datetime?

AddHours returns a new DateTime object which is the result of adding the hours onto the original. The original is left unchanged. Thus you want date = date.AddHours (-4); instead of just date.AddHours (-4); Show activity on this post.

What happens when you add hours to a date?

AddHours returns a new DateTime object which is the result of adding the hours onto the original. The original is left unchanged. Thus you want date = date.AddHours (-4); instead of just date.AddHours (-4);

What happens when you add hours to a date in Python?

AddHours returns a new DateTime object which is the result of adding the hours onto the original. The original is left unchanged. Thus you want date = date.AddHours(-4); instead of just date.AddHours(-4); The original date is set 4 hours backward but you didn't set it to new date.

What does the DATETIME () function do?

Returns a new DateTime that adds the specified number of minutes to the value of this instance. Gets the day of the week represented by this instance. Returns a new DateTime that adds the specified number of years to the value of this instance.


2 Answers

AddHours returns a new DateTime object which is the result of adding the hours onto the original. The original is left unchanged.

Thus you want date = date.AddHours(-4); instead of just date.AddHours(-4);

like image 34
Rawling Avatar answered Sep 18 '22 14:09

Rawling


Dates are immutable objects, i.e. they cannot be modified after creation. DateTime.AddHours returns a new DateTime instance which is shifted backwards by 4 hours but "date" will not be modified.

Use:

DateTime newDate = date.AddHours(-4);
like image 100
Slugart Avatar answered Sep 20 '22 14:09

Slugart