Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

This may be a widely asked question with lots of possible solutions but unfortunately none of them is working for me..

I have a string like this:
"02/09/2015 10:00"
which represents
dd/mm/yyyy HH:mm

I have tried using Convert.toDateTime() and DateTime.Parse() There is no error, but it always gives output which says its 09th Feb, 2015 which is not. The time part is alright though.

How can I get correct output?

Thank you....
Posted
Comments
CHill60 2-Sep-15 8:46am    
What code are you using to produce the output?
Andy Lanng 2-Sep-15 8:48am    
Try DateTime.ParseExact
Ralf Meier 2-Sep-15 8:52am    
Have you tried to write it like this : "dd/MM/yyyy HH:mm" instead of "dd/mm/yyyy HH:mm"
Sergey Alexandrovich Kryukov 2-Sep-15 8:55am    
What's wrong with reading original MSDN documentation?
—SA
Andy Lanng 2-Sep-15 8:57am    
Ha - virtual 5*

1 solution

First of all, think at this: can you totally avoid "conversion" (in fact, parsing) strings? This is usually possible, if your learn to work with data, not strings representing data.

To parse correctly, you just need to look at all available DateTime parse methods, not only Parse, but TryParse, ParseExact and TryParseExact. You will see parameters used to specify the culture and/or exact format:
https://msdn.microsoft.com/en-us/library/system.datetime.parse%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/System.DateTime.TryParse%28v=VS.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.datetime.parseexact%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact%28v=vs.110%29.aspx[^].

For formats, please see:
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx[^].

—SA
 
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