Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a string like "28/09/2012 04:00 PM" i want to convert it into 24hrs
(i.e dd/MM/yyyy HH:mm) format..please anybody help me out in this


Thank You in advance
Posted

Convert it to a DateTime, using ParseExact, then output it as whatever format you wish.
C#
string s = "28/09/2012 04:00 PM";
DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
s = dt.ToString("dd/MM/yyyy HH:mm");
 
Share this answer
 
Comments
chetan B Y 27-Sep-12 3:21am    
cultureInfo.InvariantCulture i am not seeing this properties...
OriginalGriff 27-Sep-12 3:30am    
You may need to add

using System.Globalization;

If you put the text cursor in the word "CultureInfo" while it has the red underline, a small blue line will appear at the beginning. Hover the mouse over the blue line, and it will show a drop down which can solve such things for you. (This is generic, not specific to CultureInfo, and a very useful feature of VS)
Hi,

try this code:
DateTime dt = Convert.ToDateTime("28/09/2012 04:00 PM");
MessageBox.Show(dt.ToString("dd/MM/yyyy HH:mm"));

See here for a reference of the available patterns:
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm[^]
 
Share this answer
 
v2
You have to use "H" or "HH"

h or hh will give time in 12 hour format
where
H or HH will give time in 24 hr format.

C#
string date=DateTime.Today.ToString("dd/MM/yyyy HH:mm");
 
Share this answer
 

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