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


MY string is in the format "22/07/2013 6:28PM". i want to convert this in the format "dd-MM-yyyy HH : mm"

that 6:28PM Should come as 18:28.

i have used the code like
C#
item.orderDateTime = DateTime.ParseExact(order1, format, CultureInfo.InvariantCulture);

in this

item.orderdatetime is of type DateTime
and order1 is of type string contains the value "22/07/2013 6:28PM"
format is "dd-MM-yyyy HH : mm"

and it is giving an error "String was not recognised as datetime format"

Not getting wat to do suggestions needed .............
Posted
Updated 21-Jul-13 21:22pm
v2

Firstly, I think you have miss-understood the purpose of the "format" criteria on the ParseExact method.
The format parameter defines how the input string (i.e. order1) should be formatted. In your case you are saying you are expecting a string in the format of "dd-MM-yyyy HH : mm" when in fact you are passing in a string in the format of "dd/MM/yyyy HH:mmtt" as a result it is failing.

Personally I would suggest using TryParse instead of ParseExact in case the strings you are receiving are not in the format you expect.
 
Share this answer
 
Comments
midnight_ 22-Jul-13 3:39am    
+1 for explaing how the ParseExact method works
+1 for using TryParse when converting from string to different culture-formats
lukeer 22-Jul-13 4:04am    
Combine them. Use TryParseExact()[^].
And afterwards, use ToString()[^] to get the format you want.
to convert the string do==>
string S= String.Format("{0:dd-MM-yyyy HH:mm}", order1);
DateTime d;
DateTime.TryParse(S, d);
item.orderDateTime =d;
Try this and Tell me if it works..

Regards,
Ibrahim Karakira
 
Share this answer
 
v2
Thanks For your suggestions but i got one more solution..........

my actual problem is i have a string like "22/07/2013 6:28PM "
i want to convert it as "22-07-1/2013 18:28"

Datetime.Tryparse() is not working prperly according to the requirement
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900