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:
From MSDN (the “h” custom format specifier):
You can use the following (as described in “Using Single Custom Format Specifiers)”:
So, you can do the following: