Solved

Past a certain time of day. C#

Posted on 2011-03-25
3
388 Views
Last Modified: 2012-05-11
What C# code can I use to determine if it's past a certain time of day?
Example, I'm getting a string type that contains 4:30pm in military time....

"16:30"
0
Comment
Question by:silentthread2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
p_davis earned 250 total points
ID: 35216867
TimeSpan.Parse(yourString) > DateTime.Now.TimeOfDay

although implementing tryparse would be more fool proof way to code
0
 
LVL 15

Assisted Solution

by:angus_young_acdc
angus_young_acdc earned 250 total points
ID: 35217064
The following code will make sure the time is in the correct format
string time = "4:30pm";
            DateTime dt = Convert.ToDateTime(Convert.ToDateTime(time).ToString("hh:mm"));

            if (dt.TimeOfDay > DateTime.Now.TimeOfDay)
            {
                // do something
            }

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35217430
Just guessing....but he probably wants to know if the CURRENT time is past the time in the string?

If so, switch the IF statement order...   =)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This is about my first experience with programming Arduino.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question