I want to verify if the current system time falls within the specified time range between 7:30pm - 9:00pm. If it falls within the specified time range then display a message. Can someone give me the code for this in vb.net. Any help appreciated.
Dim now As DateTime = DateTime.Now
Dim sevenThirty As New DateTime(now.Year, now.Month, now.Day, 19, 30, 0)
Dim nine As New DateTime(now.Year, now.Month, now.Day, 21, 0, 0)
If sevenThirty <= now AndAlso now <= nine Then
' Do something
End If
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
Do i have to use both date and time? Can i only use time along to write the logic. Does it make a difference?
It shouldnt make a difference as the date portion is being automatically picked up from the current day. So Darrel's code will work on any day.
jojupi01@yahoo.com
Can you give me a code using time alone
you could do sometihng like this if you want to only work with the time values:
Dim now As DateTime = DateTime.Now
If (now.Hour = 19 AndAlso now.Minute >= 30) OrElse now.Hour = 20 Then
'do something
End If
Note that the code above will have a subtle difference in results compared to Darrel's. This code will not evaluate to true for a time of exactly 9:00pm. it will take you from 19:00:00.0000000 to 20:59:59.9999999. Not sure if that matters for what you're
doing or not.
Either technique (datetime or time only) should work though.
jojupi01@yah...
Member
133 Points
113 Posts
Find time falls between 2 specific time range
Jun 15, 2012 03:08 PM|LINK
I want to verify if the current system time falls within the specified time range between 7:30pm - 9:00pm. If it falls within the specified time range then display a message. Can someone give me the code for this in vb.net. Any help appreciated.
Thanks,
Joe
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Find time falls between 2 specific time range
Jun 15, 2012 03:14 PM|LINK
whats the context or cirumstances when user logs-in between that time or always that message should be displayed ?
ignatandrei
All-Star
134535 Points
21583 Posts
Moderator
MVP
Re: Find time falls between 2 specific time range
Jun 15, 2012 03:16 PM|LINK
see TimeSpan
http://msdn.microsoft.com/en-us/library/system.timespan.aspx
(examples down the link)
jojupi01@yah...
Member
133 Points
113 Posts
Re: Find time falls between 2 specific time range
Jun 15, 2012 03:20 PM|LINK
only when user logs-in between that specific time range i want to display a message else no messgae will be displayed.
DarrellNorto...
All-Star
86555 Points
9624 Posts
Moderator
MVP
Re: Find time falls between 2 specific time range
Jun 15, 2012 03:20 PM|LINK
Try this:
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
jojupi01@yah...
Member
133 Points
113 Posts
Re: Find time falls between 2 specific time range
Jun 16, 2012 08:54 AM|LINK
Do i have to use both date and time? Can i only use time along to write the logic. Does it make a difference?
Can you give me a code using time alone
mbanavige
All-Star
134950 Points
15415 Posts
ASPInsiders
Moderator
MVP
Re: Find time falls between 2 specific time range
Jun 16, 2012 03:08 PM|LINK
you could do sometihng like this if you want to only work with the time values:
Dim now As DateTime = DateTime.Now If (now.Hour = 19 AndAlso now.Minute >= 30) OrElse now.Hour = 20 Then 'do something End IfNote that the code above will have a subtle difference in results compared to Darrel's. This code will not evaluate to true for a time of exactly 9:00pm. it will take you from 19:00:00.0000000 to 20:59:59.9999999. Not sure if that matters for what you're doing or not.
Either technique (datetime or time only) should work though.
jojupi01@yah...
Member
133 Points
113 Posts
Re: Find time falls between 2 specific time range
Jun 18, 2012 03:43 PM|LINK
Thanks all. Your replies was very helpful.