Convert String to DateTime in C# - String was not recognized as a valid DateTime -
i reviewed question here: convert string datetime in c#.net
the format i'm trying pass different, cannot work.
my code:
var date = datetime.parseexact(@"28/06/2012 06:04:10 pm", @"dd/mm/yyyy hh:mm:ss tt", null);
so tried example code question mentioned above:
var date = datetime.parseexact(@"14/04/2010 10:14:49.pm", @"dd/mm/yyyy hh:mm:ss.tt", null);
that doesn't work either. both give me
system.formatexception string not recognized valid datetime.
any ideas appreciated! thanks!
the problem localisation.
consider these 3 statements:
datetime.parseexact(@"14/04/2010 10:14:49.pm", @"dd/mm/yyyy hh:mm:ss.tt", new cultureinfo("fr-fr")) datetime.parseexact(@"14/04/2010 10:14:49.pm", @"dd/mm/yyyy hh:mm:ss.tt", new cultureinfo("en")) datetime.parseexact(@"14/04/2010 10:14:49.pm", @"dd/mm/yyyy hh:mm:ss.tt", cultureinfo.invariantculture)
the first not work whereas last 2 will. in case because pm
not valid in fr-fr
. if try this:
datetime.parseexact(@"14/04/2010 10:14:49.", @"dd/mm/yyyy hh:mm:ss.tt", new cultureinfo("fr-fr"))
it work fine.
as has been noted in comments other cultures may fail on other items. en-za
uses different date separator causing fail.
Comments
Post a Comment