I have my TimeSpan
for a specific reason so it HAS to be in that format. I'm trying to add an hour on to the current time. Here is what I got, which does not work:
TimeSpan time1= TimeSpan.FromHours(1); // my attempt to add 2 hours TimeSpan ts = DateTime.Now.TimeOfDay; ts.Add(time1); MessageBox.Show(ts.ToString()); // for showing me its result
Can you please advise?
Add() Method in C# This method is used to a get new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance. Syntax public TimeSpan Add (TimeSpan t);
It should be var totalSpan = new TimeSpan(myCollection. Sum(r => r. Ticks)); in case if myCollection is List<TimeSpan>() .
A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date.
To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property.
ts += TimeSpan.FromHours(1);
and there you have it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With