# compare Time-Span with date-time RSS

## 6 replies

Last post May 01, 2012 06:31 PM by gerrylowry

Member

20 Points

23 Posts

### compare Time-Span with date-time

i want to convert this value to date time or compare this value to CurrentDate Time

Contributor

3230 Points

668 Posts

### Re: compare Time-Span with date-time

For checking for a time of day use:

TimeSpan start = new TimeSpan(10, 0, 0); //10 o'clock
TimeSpan end = new TimeSpan(12, 0, 0); //12 o'clock
TimeSpan now = DateTime.Now.TimeOfDay;

if ((now > start) && (now < end))
{
//match found
}
For absolute times use:

DateTime start = new DateTime(2009, 12, 9, 10, 0, 0)); //10 o'clock
DateTime end = new DateTime(2009, 12, 10, 12, 0, 0)); //12 o'clock
DateTime now = DateTime.Now;

if ((now > start) && (now < end))
{
//match found
}

Regards

( Mark as Answer if it helps you out )

View my Blog

Star

13431 Points

2535 Posts

### Re: compare Time-Span with date-time

What do you mean?  If you compare time span of 2 days, 3 hours and 47 mintues to the the current date and time what answer would you like?  It doesn't make any sense to me.

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

Member

20 Points

23 Posts

### Re: compare Time-Span with date-time

Dear Paul Linton ,

I mean i have also days in timespan value and i want to compare with currnent date time is a time span days , hour,mint  and second greater than dateTime.now or less.

Contributor

3230 Points

668 Posts

### Re: compare Time-Span with date-time

hi,

DateTime dtts;

TimeSpan ts = new TimeSpan(-2, 20, 09, 39, 4468381); // instead you can use your timespan variable directly

dtts = Convert.ToDateTime(ts.ToString());

int result = DateTime.Compare(dtts, DateTime.Now);

if (result < 1)

{

// timespan is earlier than todays datetime

}

else if (result == 0)

{

// timespan is equal to todays datetime

}

else if (result > 1)

{

// timespan is later than todays datetime

}

Regards

( Mark as Answer if it helps you out )

View my Blog

Star

13431 Points

2535 Posts

### Re: compare Time-Span with date-time

Can you give some examples of TimeSpans and DateTimes and how you want them to compare?  For example, the example you gave in the original post, what DateTime is that equal to?

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

All-Star

20515 Points

5713 Posts

### Re: compare Time-Span with date-time

for me, what you are asking is not clear.

#### mjaved39

I mean i have also days in timespan value and i want to compare with currnent date time is a time span days , hour,mint  and second greater than dateTime.now or less.

FWIW, a TimeSpan type is not a DateType type.

http://msdn.microsoft.com/en-us/library/system.timespan.aspx

"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.