-1

Is it possible to compare a timespan to an integer in C#?

if say I want to know if a timespan is equal to 30 days, and if it is then do something?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ida
  • 11
  • 1

3 Answers3

3

There is the property TotalDays

There's also TotalHours, TotalMinutes, TotalSeconds and TotalMilliseconds. You should check out the TimeSpan-properties for more information

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
peter
  • 14,348
  • 9
  • 62
  • 96
1

You can use the property Days of the TimeSpan object, which returns the days component of the time interval

if (ts.Days == 30)
{
   // do something
}
1
TimeSpan t = new TimeSpan();
if(t.TotalDays==30)
{
//Do Something
}
Detlef D Soost
  • 83
  • 3
  • 11
  • Please explain your code and how it solves the OP's question (https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) – Nic3500 Aug 01 '18 at 12:44
  • 1
    found the right solution for me thanks for the help – Ida Aug 17 '18 at 08:22