Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I need your help here.
I need to convert this string to date time : "20/May/2013 05:51:18 +0000"

I am using below code to do so,
DateTime dt = Convert.ToDateTime("20/May/2013 05:51:18 +0000");

but I am getting this result : "5/20/2013 11:21:18 AM"
the expected result is : "5/20/2013 05:51:18 PM"

I also tried it by removing +0000 from the string and got below result,

DateTime dt = Convert.ToDateTime("20/May/2013 05:51:18");

Output dt : "5/20/2013 05:51:18 AM" ( I need it in PM : "5/20/2013 05:51:18 PM")

I also tried it using the Parseexact method but getting the same results.
I need the above output to be in PM because the string I have provided is in PM (05:51:18 PM).

Any help would be appreciated.

Thanks in advance,
Kane
Posted
Comments
joshrduncan2012 20-May-13 9:47am    
My suggestion would be trying military time or changing the 0s to the correct time zone difference.

No, you are getting exactly the result I would expect - 05:51:18 is 5AM, not PM - 17:51:18 would be 5PM.

If you do not specify that the time is 12 hour, then it will assume AM at all times - because it would be silly to assume PM, when the input could well be 24 hour.

Adding the "+0000" logs the time as UTC (which in this case is GMT plus zero) so when you print it out without specifying a time zone you get your PC locale, and the UTC is corrected to that: in the UK for example, an hour will be added because we are on Summer Time, which ist GMT + 1 hour.

If you want to convert the time to 5PM, then you need to supply PM indicators, and quite possibly specify the locale as well, if you are using UTC as in your first example.
 
Share this answer
 
Comments
kanekhan 20-May-13 11:27am    
I am taking this value from a web server log file which I need to parse and it was logged at 05:51:18 PM exactly. Hence it is a PM value not AM.

Could you please tell me how to parse it to datetime in PM itself.
OriginalGriff 20-May-13 11:37am    
If the log server didn't add any indicator, then how are you sure? What would it have added at 5AM?
Just saying you need to be careful - if it doesn't save any indicator, then you would have to assume they are all AM or all PM. Without info to tell you which, you would have to guess! :laugh:
kanekhan 21-May-13 3:08am    
I did an activity on the application at 05:51:18 PM and it was logged in the web server log file exactly at 05:51:18 PM hence I know it is a PM not an AM.
OriginalGriff 21-May-13 3:34am    
Unfortunately, that proves absolutely nothing, and doesn't help you at all!
If the we server logged it, it logged it in it's time or UTC, not necessarily the same time as your PC. Unless it saves the AM PM indicators in the log file, you cannot tell if that was 05:51 or 17:51 in the web server local time - which depending on it's physical location may be in a totally different time zone!

Do you have any other entries in the log file that might be twelve hours earlier? Or later?
OriginalGriff 21-May-13 3:36am    
Alternatively, I notice you say "application" - is that what is saving the log file data, from your local PC? If so, then it may not be saving AM/PM info, in which case you are right back to guessing - or changing the application!
Please replace "+0000" with "PM". I just tested it with above mentioned time string.
 
Share this answer
 
Comments
kanekhan 20-May-13 11:28am    
Hi Mahesh,

I guess that won't be a solution because in that case it would parse it to PM only even if the value is AM.

Any other solution if you have.

Regards

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