Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two strings representing 12 hour clock time,
say 10AM and 5PM.
In order to compare them i need to convert the format of the string to 24 hour clock format.
After comparison, again convert the timings back to 12 hour clock format.
c#
Posted

C#
DateTime d = DateTime.Now;
            // 12 hour format
            string aaa = d.ToString("hh:mm");
            // 24 hour format
             string aa =d.ToString("H:mm");

Happy Coding!
:)
 
Share this answer
 
use this string to convert 24 hour format to 12 hour format:

string 12hrFormat = DateTime.Today.ToString("dd-MM-yyyy") + " " + DateTime.Today.ToString("hh:mm tt");
 
Share this answer
 
Something like
C#
string [] s ={"10AM", "5PM"};
DateTime [] h = {DateTime.Parse(s[0]), DateTime.Parse(s[1])};

if (h[0] > h[1])
  Console.WriteLine("{0} is later than {1}", s[0], s[1]);
else
  Console.WriteLine("{0} is earlier than {1}", s[0], s[1]);

?
 
Share this answer
 
v2

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