-5

Guys I want to add Minutes to time for exemple I have 8:50h and I want to add to it another 8:50h; I have tried

time = time.Add(TimeSpan.FromHours(8.5)); // time variable already has 8:50:00 and time it's TimeSpan variable

but I got 17:00h (it treats it like floats) but I want it to be 17:40 (so every 60mins it will add an hall hour).

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
stacj aa
  • 611
  • 2
  • 9
  • 17
  • 1
    https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addminutes?view=net-5.0 Try use `AddMinutes` instead of simple `Add` – demo Mar 02 '21 at 16:10
  • 3
    `8.5`!=`8:50`; `8.5`==`8:30` – Magnetron Mar 02 '21 at 16:13
  • 1
    @MongZhu microsoft should detect better my location... or add support of more languages – demo Mar 02 '21 at 16:13
  • @demo just edit the link to remove the `/ru-ru` bit, then it autodetects correctly. – Ian Kemp Mar 02 '21 at 16:14
  • "(so every 60mins it will add an hall hour)." I find this a very confusing comment. What result do you expect if `time` has the value `3:00` ? up to now your problem is simply solveable by this line: `timeSpan += timeSpan;` – Mong Zhu Mar 02 '21 at 16:19

1 Answers1

5

You don't understand how time and date works in programming. 8.5 is not 8 hours 50 minutes, it's 8 hours 30 minutes because .5 is half of one hour.

The correct way to do this is:

var ts1 = new TimeSpan(8, 50, 0); // 8 hours, 50 minutes, 0 seconds
var ts2 = new TimeSpan(8, 50, 0);
var ts3 = ts1.Add(ts2); // 17:40
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138