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.
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.
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?
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.
"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.
Please help us help you by taking time to describe your problem better ... sorry, but what you are asking is quite unclear imho.
To compare apples to oranges, it is important to have some common property such as the weight of the apples versus the weight of the oranges.
Likewise, to compare a TimeSpan with DateTime.Now is just not logical.
mjaved39, you must compare a TimeSpan with another TimeSpan.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
mjaved39
Member
20 Points
23 Posts
compare Time-Span with date-time
Apr 26, 2012 05:15 PM|LINK
Dear all please help me i have timespan value like -2.20:09:39.4468381 in timespan variable
i want to convert this value to date time or compare this value to CurrentDate Time
tusharrs
Contributor
3230 Points
668 Posts
Re: compare Time-Span with date-time
Apr 26, 2012 05:18 PM|LINK
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
}
( Mark as Answer if it helps you out )
View my Blog
Paul Linton
Star
13581 Points
2571 Posts
Re: compare Time-Span with date-time
Apr 26, 2012 10:49 PM|LINK
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.
mjaved39
Member
20 Points
23 Posts
Re: compare Time-Span with date-time
Apr 27, 2012 04:29 AM|LINK
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.
tusharrs
Contributor
3230 Points
668 Posts
Re: compare Time-Span with date-time
Apr 27, 2012 04:57 AM|LINK
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
}
( Mark as Answer if it helps you out )
View my Blog
Paul Linton
Star
13581 Points
2571 Posts
Re: compare Time-Span with date-time
Apr 27, 2012 06:56 AM|LINK
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?
gerrylowry
All-Star
20577 Points
5721 Posts
Re: compare Time-Span with date-time
May 01, 2012 06:31 PM|LINK
@ mjaved39
for me, what you are asking is not clear.
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.
Please help us help you by taking time to describe your problem better ... sorry, but what you are asking is quite unclear imho.
To compare apples to oranges, it is important to have some common property such as the weight of the apples versus the weight of the oranges.
Likewise, to compare a TimeSpan with DateTime.Now is just not logical.
mjaved39, you must compare a TimeSpan with another TimeSpan.
g.