Click here to Skip to main content
15,886,713 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How do i get military time and not the date. currently it returns this.

11/25/2016 1:30:00 AM

I want to see
13:30


C#
var start_time = dateTimePicker1.Text;
var start_time_array = start_time.Split(':');
var today = DateTime.Now;
var trailer_count = Convert.ToInt32(tbTrailer_Needed.Text);
var minutes_apart = Convert.ToDouble(tbTime_Between.Text);
var calculated_start_time = new DateTime(today.Year, today.Month, today.Day, Convert.ToInt16(start_time_array[0]), Convert.ToInt16(start_time_array[1]), 00);


What I have tried:

None of these worked
I have tried Mask and adjusting property.
var time = value.TimeOfDay;
C#
DateTime.Now.ToString("hh:mm:ss")
Posted
Updated 26-Nov-16 17:48pm
v2
Comments
Patrice T 25-Nov-16 19:10pm    
"1:30:00 AM" is more likely to give "13:30" rather than "14:30"
Peter_in_2780 25-Nov-16 19:59pm    
and 01:30 would be even better
Member 12349103 25-Nov-16 20:19pm    
I corrected the typo any suggestions on how to fix it?
PIEBALDconsult 25-Nov-16 20:49pm    
There's no such thing as "military time".
Patrice T 25-Nov-16 21:39pm    
In some country, the 24 hours format is called military time because the format in usage is 12 hours format with AP/PM.

If you want military time, you have to use ToString("HH:mm")
 
Share this answer
 
Comments
Member 12349103 25-Nov-16 21:07pm    
Getting error when add to my code. an object reference is required for a non static field, method or property to datetime.ToString(string)
Dave Kreskowiak 25-Nov-16 22:01pm    
You put the ToString() call on your variable that is tracking your time.

    Console.WriteLine("The time is " + myTime.ToString("HH:mm"));
PIEBALDconsult 26-Nov-16 0:14am    
Console.WriteLine("The time is {0:HH\\:mm}" , myTime);
Dave Kreskowiak 26-Nov-16 0:24am    
    Console.WriteLine($"The time is {myTime.ToString("HH:mm")}");
PIEBALDconsult 26-Nov-16 1:07am    
Console.WriteLine("What have you been smoking?");
Help yourself to the Custom Date and Time Format Strings[^] in .NET framework.
Specifically, your interested military time (24-hour clock) format is at The "HH" custom format specifier[^].
 
Share this answer
 
v2
If you want it as an integer (eg:1330 hours), as it is often used

C#
int time24 = mtTime.Hour * 100 + myTime.Minute;
 
Share this answer
 
Comments
Richard MacCutchan 26-Nov-16 4:00am    
Why not just:
myTime.ToString("HHmm"); ?
Member 12349103 26-Nov-16 11:23am    
I dont know what lines to edit in my code to make it work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900