-1

I'm have to get the initial and the last hour of a TimeSpan variable. Is it possible to obtain? I haven't found anything talking about this.

2 Answers2

2

I think you misunderstand the purpose of TimeSpan.

It does not represent "From a specific time to a specific time".

It represents a length of time, like "5 minutes" or "6 hours".

To represent "a specific time to a specific time" you need to use two DateTime objects: one for the start time, and one for the end time.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • 1
    That's exactly what I thought. The user case specified that I need to use the TimeSpan that the other company sent me to work with the information. Thank you! – Igor Formiga Mar 10 '18 at 07:37
2

A TimeSpan is a measure of time, how long some event took. Like how long did it take to run a race. How long do you boil pasta for. At what interval do you want an event to execute. You could not find the initial hour and last hour of any of those.

What is possible is to apply a TimeSpan instance to a DateTime instance. The initial DateTime being when the event started and the resulting DateTime when the event ended. Only looking at hour though, as you mention in your question, could lead to bad results if the start and end occur on different days.

Example:

TimeSpan duration = new TimeSpan(3, 0, 0); // 3 hours
DateTime start = new DateTime(2017, 5, 12, 16, 25, 0); // starts on
DateTime end = start.Add(duration); // ends 3 hours from start
Igor
  • 60,821
  • 10
  • 100
  • 175