Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:11:18+00:00 2026-05-30T04:11:18+00:00

I am writing a service that calls a method at 4pm and 5am. I

  • 0

I am writing a service that calls a method at 4pm and 5am. I would like to keep these times from being hard coded and so I would like to have put them in my appconfig.

Here is the code I have:

public bool CheckTime() 
        {
            DateTime startTime;
            DateTime endTime; 

            DateTime.TryParse(ConfigurationManager.AppSettings["ProcessingStartTime"], out startTime);
            DateTime.TryParse(ConfigurationManager.AppSettings["ProcessingEndTime"], out endTime);

            if(DateTime.Now.TimeOfDay.Equals(startTime) || DateTime.Now.TimeOfDay.Equals(endTime))
                return true;
            else
                return false;
        }

But, since I only want to store the time as a string (something as easy as “4:00pm”) how do I parse and encapsulate JUST the time in a DateTime object? Or is there another object? I don’t care about the date, it just has to be M-F of any given week of any given year.

Thanks guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:11:20+00:00Added an answer on May 30, 2026 at 4:11 am

    You can use TryParseExact with an appropriate format string, and then ignore the date. There’s no nice encapsulation for “just a time” in .NET. However, there’s an alternative – I’ve been working hard on Noda Time which has the LocalTime type for just this purpose:

    // "Traditional" way
    LocalTime time;
    if (LocalTime.TryParseExact(text, "h:mmtt", out time))
    {
        ...
    }
    
    // Preferred way
    
    private static readonly LocalTimePattern TimePattern = 
        LocalTimePattern.CreateWithInvariantInfo("h:mmtt");
    ...
    
    ParseResult<LocalTime> parseResult = TimePattern.Parse(text);
    if (parseResult.Success)
    {
        LocalTime time = parseResult.Value;
        ...
    }
    

    Two side-notes:

    • It’s almost never appropriate to ignore the return value from DateTime.TryParse (or any other TryParse method) – do you really want to use DateTime.MinValue if there’s a problem with the data? There are cases where the default value is appropriate, but they’re pretty rare
    • Please try to avoid code of the form:

      if (condition)
          return true;
      else
          return false;
      

      This is much more clearly written as:

      return condition;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Windows Service that needs to take a file from an FTP
I'm writing some code that calls a web service, reads back the response and
I am writing a C# client that calls a web service written in Java
I am writing WCF service that uses wsHttpBinding binding, which is not hosted in
I have the following data model: I am writing a WCF service that needs
I'm writing a windows service application that needs to serialize and deserialize XML documents
I'm writing JUnit tests for a service that creates and acts on an entity
I'm writing in .NET a windows service that, at windows startup, executes some network
I have a WCF service that is responsible for writing a log file. I
I'm writing a server running as a Windows service that by request invokes Firefox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.