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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:33:25+00:00 2026-05-15T08:33:25+00:00

I want to get the hours from the datetime. So if it is 1pm

  • 0

I want to get the hours from the datetime. So if it is 1pm it will just be 1 if it is 10pm it will be 10. So no leading zero to be place on hours 1-9

So I tried to do this

DateTime test= DateTime.Now;
Console.WriteLine(test.ToString("h"));

I get this

System.FormatException was unhandled
Message=Input string was not in a
correct format. Source=mscorlib
StackTrace:
at System.DateTimeFormat.GetRealFormat(String
format, DateTimeFormatInfo dtfi)
at System.DateTimeFormat.ExpandPredefinedFormat(String
format, DateTime& dateTime,
DateTimeFormatInfo& dtfi, TimeSpan&
offset)
at System.DateTimeFormat.Format(DateTime
dateTime, String format,
DateTimeFormatInfo dtfi, TimeSpan
offset)
at System.DateTimeFormat.Format(DateTime
dateTime, String format,
DateTimeFormatInfo dtfi)
at System.DateTime.ToString(String
format)
at ConsoleApplication1.Program.Main(String[]
args) in
C:\Users\chobo2\Documents\Visual
Studio
2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line
21
at System.AppDomain._nExecuteAssembly(RuntimeAssembly
assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String
assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback
callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback
callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

  • 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-15T08:33:25+00:00Added an answer on May 15, 2026 at 8:33 am

    From MSDN (the “h” custom format specifier):

    If the “h” format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

    You can use the following (as described in “Using Single Custom Format Specifiers)”:

    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.

    So, you can do the following:

    DateTime test= DateTime.Now;
    Console.WriteLine(test.ToString("{0:%h}")); // From the document, adds precision
    Console.WriteLine(test.ToString("%h")); // Will also work
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get Only Hours From date by using PHP & Cakephp function.
I want to get the previous day (24 hours) from the current time. e.g
I want to get the business hours from ScotiaBank branches that are near to
I want get as much as possible from Redis + Hiredis + libevent. I'm
I just want get a 2 dimension array of List in c#. In my
I want to get show information from myspace artists. One way I could do
Just want to get some views/possible leads on an issue I have. I have
I found this snippet: def timesince(dt, default=just now): now = datetime.utcnow() diff = now
I have a DateTime column. I want to extract all records, lets say, from
I've been trying to get this working for a couple of hours now but

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.