Reputation: 432
I've got following problem.
I set CurrentCulture
and CurrentUICulture
and use following patterns in them:
ShortDatePattern
is dd-MM-yyyy
LongTimePattern is HH.mm.ss
.
When I convert dates to string I get 15-01-2008 00.00.00
. But when i call DateTime.Parse("15-01-2008 00.00.00")
it throws a FormatException
. If i set ShortDatePattern
to dd-MM-yyyy HH.mm.ss
exception is still thrown. Is there any way to force DateTime.Parse
to use pattern for time by setting CurrentCulture
accordingly.
I know that using Parse overloads or ParseExact
might help, but the whole point was to use formatting without refactoring
loads of code that is already written and uses DateTime.Parse
and ToString
all over the place
Additional info: A also tried putting -
and .
in '
- it was no use. CurrentCulture
is based on Swedish
.
Upvotes: 0
Views: 680
Reputation: 18843
if you want to format a DateTIme using String.Format()
Method you could do something like this below. String.Format DateTime C#
var dt = "15-01-2008 00.00.00";
var dateFrmt = String.Format("{0:dd/MM/yyyy HH:mm:ss}", dt);
Output = "15-01-2008 00.00.00"
if you want to strip the HH:mm:ss out of the DateTime variable for short date you could do the following here is an example will yield ShortDate
DateTime dt = DateTime.Now;
var dateFrmt = String.Format("{0:M/d/yyyy}", dt);
Output = "1/9/2013"
DateTime.ParseExact Method if you choose to go that route
Upvotes: 1