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

  • Home
  • SEARCH
  • 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 7022523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:35:37+00:00 2026-05-27T23:35:37+00:00

It just doesn’t want to work: DateTime time; DateTime.TryParseExact(17, HH, …, out time); //

  • 0

It just doesn’t want to work:

DateTime time;

DateTime.TryParseExact("17", "HH", ..., out time);
  // works fine

DateTime.TryParseExact("9", "HH", ..., out time);
  // works fine, but 9 doesn't match HH (nor should it)

DateTime.TryParseExact("9:", "H':'", ..., out time);
  // works fine

DateTime.TryParseExact("9", "H", ..., out time);
  // exception: "Input string was not in a correct format"

The fact that #3 works offers an obvious work-around, but it’s one of those things that would make me go “WTF” if I saw it in someone else’s code. Is TryParseExact buggy or something?

  • 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-27T23:35:38+00:00Added an answer on May 27, 2026 at 11:35 pm

    You can use DateTime.TryParseExact("5", "%H", null, DateTimeStyles.None, out time).

    To parse the hour in a 24-hour clock without leading zero one could be tempted to use just the "H" format, but a custom date and time format must consist of two or more characters, which would lead to "H" being interpreted as a standard date and time format, resulting in a format exception.

    From MSDN on Custom Date and Time Format Strings:

    To use any of the custom date and time format specifiers as the only
    specifier in a format string (that is, to use the “d”, “f”, “F”, “g”,
    “h”, “H”, “K”, “m”, “M”, “s”, “t”, “y”, “z”, “:”, or “/” custom format
    specifier by itself), include a space before or after the specifier,
    or include a percent (“%”) format specifier before the single custom
    date and time specifier.

    I prefer to include the % sign before since I find that a space before or after may be interpreted as a typing mistake and be removed by someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must be making some obvious mistake but it just doesn't seem to work.
I know this has been asked before, but it just doesn't work for me.
i'm flabbergasted, i've looked at almost every example, but it just doesn't work (the
I want to add a simple route to my webapp, but it just doesn't
I have this code, it works but it just doesn't look right. I'm creating
I'm trying to make a simple stopwatch but it just doesn't work.. The app
I have tried the following code in many ways but it just doesn't work.
So a UISlider works great to change/choose values but it just doesn't look that
Maybe it's just doesn't exist, as I cannot find it. But using python's logging
Rails is a great platform, but it just doesn't have the history that Perl

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.