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 6168513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:42:04+00:00 2026-05-23T22:42:04+00:00

Possible Duplicate: Get just the hour of day from DateTime using either 12 or

  • 0

Possible Duplicate:
Get just the hour of day from DateTime using either 12 or 24 hour format as defined by the current culture

I’m having some problems formatting my DateTime-objects so that only the full hours are shown. For example if I have the following DateTime:

? StartTime
{7/25/2011 8:00:00 AM}

I would like to print this out as “8 AM”. The formatting should also follow the rules of the current culture, so in other countries the above example could be printed as “8”. And 4 PM would be shown as 16.

I haven’t found any built-in formatters to do this so I thought about writing some static method to handle the problem. I need to get the hours in the correct format (single or two digits) and the string designator for the given hour. String designator is easy with the ToString(“tt”) and this correctly returns empty if the current culture uses the 24h format. So I’m half way there.

But then I’m stuck because the CultureInfo’s DateTimeFormatInfo doesn’t seem to tell me if I should show the hours with one or two digits (h vs H).

Any ideas?

  • 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-23T22:42:05+00:00Added an answer on May 23, 2026 at 10:42 pm

    I think you can use the hh format combined with tt.

    DateTime x = new DateTime(2011,10,10,08,00)
    Console.WriteLine(x.ToString("h tt")); // Output: '8 AM'
    
    x = new DateTime(2011,10,10,16,08,00)
    Console.WriteLine(x.ToString("h tt")); // Output: '4 PM'
    

    Have a look at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#Y16327

    UPDATE:

    I think Get just the hour of day from DateTime using either 12 or 24 hour format as defined by the current culture gives you the answer how to display the hours according to the CultureInfo (either 12 or 24 hours).

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

Sidebar

Related Questions

Possible Duplicate: Get just the Date from grouping in select from DateTime column in
Possible Duplicate: Get Client IP using just Javascript? I know PhP can know your
Possible Duplicate: Get all files from VSS for a given date? I need to
Possible Duplicate: How can I get a list of all the defined variables in
Possible Duplicate: Return value from thread I want to get the free memory of
Possible Duplicate: Data transfer from JavaScript to PHP. How can i get the browser's
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know
Possible Duplicate: How to get the URL of the current page in C# If
Possible Duplicate: Get list of all input objects using JavaScript, without accessing a form
Possible Duplicate: get current system volume level on iPhone I am working on an

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.