Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey it's me again. I'm happy because all of you help me to make my job easier.

I have a simple but big problem:
//a date with an hour
DateTime date = DateTime.Parse("2011-02-01 00:23:50");
//I tried doing this too:
DateTime date = new DateTime(2011,2,1,0,23,50);

//It doesn't work and I don't know why:
date.AddHour(-1);
// It doesn't work eighter
date.AddDay(1);


I want to substract an hour from this datetime I should have something like this: 2011-01-31 23:23:50
The fact is that I want to add or substract anything I want (a year, day, month, hour, minute or second) and not just an hour. Is there any automatic function?

Thanks!
Posted
Updated 2-Feb-11 11:59am
v2

Don't forget, time and duration are different things. You also need duration. Time is represented by DataTime, but duration — more powerful for time arithmetics — by TimeSpan. Is my hint already taken?

If not, please keep reading:

C#
DateTime time = DateTime.Now;
int days = 13;
int hours = 12;
int minutes = 55;
int seconds = 12;
time -= new TimeSpan(days, hours, minutes, seconds);

//lookup all the overloaded constructors for more detail...

time += new TimeSpan(days, hours);


Now the idea should be completely clear for you. With TimeSpan, you can do any reasonable time arithmetic operation you might expect.

Side note: one useful application of such arithmetic operations is measuring time: you can subtract one time from another, returned result is TimeSpan:

C#
DateTime before = DateTime.Now;
//some timely operation here
DateTime after = DateTime.Now;

TimeSpan delay = after - before;
double ms = delay.TotalMilliseconds;
double hours = delay.TotalHours;

//...



—SA
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 2-Feb-11 22:22pm    
The vote of "2" is outrageous, even funny. How is so clever? :-)
--SA
Sandeep Mewara 2-Feb-11 23:57pm    
Timespan is the most appropriate way! Countered.:)
JF2015 3-Feb-11 2:27am    
Good and very detailed answer - as always. 5+
pancho2413 7-Feb-11 9:16am    
You are right. Even when I just need to substract a day, your answer is more complete for any case. Thanks a lot friend.
Sergey Alexandrovich Kryukov 7-Feb-11 21:29pm    
Or finally! I hope you would realize that.
Thank you for accepting my answer.
Good luck, call again,
--SA
OH MIGHTLY GOD!

I didn't realize it was such an easy solucion:

date.AddHour(-1) is a function that returns a DateTime with the new information, so if I don't assign the new value to any variable I will have the same problem forever:

here is the solution:

date = date.AddHour(-1);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Feb-11 21:40pm    
Unfortunately, this is not universal solution you were asking for.
You can find the solution in my answer.
--SA
Sandeep Mewara 2-Feb-11 23:59pm    
Surely one of the way, but this would only go for defined hours. (or similar for minutes or seconds) BUT not for all of them at once.

If you really want to add/remove some hours-minutes-seconds from a given DateTime then Timespan is the way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900