I have this in my code:
var date1 = DateTime.ParseExact(date, "dd.MM.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
And when my current cultur is dutch (nl-NL
) instead of May 1st I get January 5th.
I think the error is in the second parameter dd.MM.yyyy HH:mm:ss
.
Is there a way to fix this using the CultureInfo
class?
A DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.
You can use date1. ToString(CultureInfo. CreateSpecificCulture("en-US")) .
The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.
The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.
You may try the following:
System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("nl-NL"); DateTime dt = DateTime.Parse(date, cultureinfo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With