Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
How to convert a string to datetime in 24 hour format? I have tried several methods but still I get 12 hour format value. Eg: String strDate = "24/01/2013 00:00:00"
DateTime date = DateTime.ParseExact(strDate, "dd/MM/YYYY HH:mm:ss", CultureInfo.InvariantCulture);
But the value is 24/01/2013 12:00:00. Instead I need "24/01/2013 00:00:00". Please help out :(.
Posted
Updated 17-Jul-19 22:49pm
Comments
Patrice T 20-Aug-15 10:21am    
I suspect you ask a reversed question.
I think your question should be :
"Convert a string in 24 hour format to datetime in c#"
User1454 21-Aug-15 0:49am    
:)

The DateTime type[^] does not have a format. The value is stored as the number of "ticks" (100 nanoseconds) since midnight, January 1, 0001 A.D. (C.E.) in the Gregorian calendar.

What you are seeing is the debugger's representation of that value, which converts the DateTime to a string using a standard format.

When you display the DateTime to the user, you can use the standard[^] or custom[^] format strings to display it any way you want.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Aug-15 10:15am    
5ed.
—SA
Maciej Los 20-Aug-15 17:23pm    
+5!
User1454 21-Aug-15 1:14am    
Sir, I have gone thru that "Standard Date and Time Format Strings" and "Custom Date and Time Format Strings", was very very helpful to me... !!!!!!!! thnx a lot !!!
String - I get a Format Exception, because "YYYY" is not a known format code: it should be:
C#
String strDate = "24/01/2013 00:00:00";
DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Which gives me:
24/01/2013 00:00:00

I suspect your output is formatting the data to 12 hour (AM/PM) format instead:
C#
Console.WriteLine(date.ToString("dd/MM/yyyy hh:mm:ss"));
So try:
C#
Console.WriteLine(date.ToString("dd/MM/yyyy HH:mm:ss"));

Which might be misleading you.
 
Share this answer
 
Comments
User1454 20-Aug-15 10:23am    
Sorry for that !!!, i have given "yyyy" only, but still it gives me "15-08-2015 AM 12:00:00". I have a doubt whether..in anyway will this take the system format because whatever value I get is exactly according to my system format. If I change to 24 hour format in my pc, I get the required one.
OriginalGriff 20-Aug-15 10:34am    
That's exactly what it is - if you don;t specify a format, then the current system one is used - and your PC is configured to use AM/PM so that will show. Mine is set to 24Hr, so I get the right display.
The DateTime stored will be exactly the same (as it's a number of milliseconds since an arbitrary point in time, not a formatted date and time).
Richard Deeming 20-Aug-15 11:58am    
It's a number of ticks (100 nanoseconds), not milliseconds. :)
User1454 20-Aug-15 10:41am    
Sir, so u meant like that I should specify the format like how you have provided in your answer (date.ToString("dd/MM/yyyy hh:mm:ss"))? is that so?
OriginalGriff 20-Aug-15 10:58am    
That's only for presentation to the user - calculations within your app should use the DateTime value which doesn't have any format associated with it.

The two format strings I showed give different results: "hh:mm:ss" will always give AM/PM format, regardless of the computer settings; "HH:mm:ss" will always give 24Hr format. Have a look here:
http://www.codeproject.com/Tips/54577/Formatting-a-DateTime-for-display-format-string-de
It shows the various formats and what they do.
If you don't specify a format at all, then the current PC settings will always be used. So for debug display in VS and so forth, you always get your system settings.
Once you have successfully parsed into a DateTime structure, you can use the .NET string formatting to display it in the format you want.

Custom Date and Time Formatting[^]

C#
String strDate = "24/01/2013 00:00:00"
String strDate = "01/24/2013 00:00:00";

DateTime date = DateTime.Parse(strDate);

Console.WriteLine("My Date: {0:dd/MM/yyyy HH:mm:ss}", date);
 
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