469,338 Members | 8,115 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

How to extract time only from DateTime.Now, and compare it with

I have code below. But it won't compile.
Error:

Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'

DateTime now = DateTime.Now.TimeOfDay;

DateTime openTime = Convert.ToDateTime("9:30:00");

if (now < openTime)
{
// Do nothing if it's before 9:30 AM
return;
}
Oct 21 '08 #1
3 15774
"Curious" <fi********@yahoo.comwrote in message
news:c9**********************************@m32g2000 hsf.googlegroups.com...
>I have code below. But it won't compile.
Error:

Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'

DateTime now = DateTime.Now.TimeOfDay;

DateTime openTime = Convert.ToDateTime("9:30:00");

if (now < openTime)
{
// Do nothing if it's before 9:30 AM
return;
}
TimeOfDay returns a TimeSpan and you're assigning it to a DateTime. Try:

TimeSpan now = DateTime.Now.TimeOfDay;

TimeSpan openTime = TimeSpan.Parse("0.09:30:00");

if (now < openTime)
{
return;
}

Oct 21 '08 #2
Thanks John!

It works! Could you tell me why you use TimeSpan.Parse("0.09:30:00")
instead of TimeSpan.Parse("9:30:00") ?

Oct 22 '08 #3
"Curious" <fi********@yahoo.comwrote in message
news:c4**********************************@v30g2000 hsa.googlegroups.com...
Thanks John!

It works! Could you tell me why you use TimeSpan.Parse("0.09:30:00")
instead of TimeSpan.Parse("9:30:00") ?
The leading "0." is the number of days in the TimeSpan, I just included it
to have a complete specification.

Oct 23 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by scott | last post: by
3 posts views Thread by laurenq uantrell | last post: by
1 post views Thread by Tim::.. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.