How to use IFormatProvider in DateTime.ParseExact - c#

Why should we use IFormatProvider in DateTime.ParseExact if there is already a format parameter?
DateTime.ParseExact(inputString, format, cultureInfo);

The format parameter says what pattern to use - but it doesn't say anything about which calendar, month names, short date format etc to use. That's up to the IFormatProvider.
For example, suppose you wanted to parse a value with the pattern "dd MMMM yyyy" - which month names would you expect to work? If you're using a month name of "February" but you're running on a machine with a system culture of French, it would fail - you'd need to specify an English culture (or the invariant culture) to get it to work. Likewise, you could specify a pattern of "d" to mean the short date format - but which short date format?
Even the calendar you use is affected by the format provider: the value could be parsed into the same year, month and day values in two cultures - but the meaning of those values would be very different in a Hijri calendar from a Gregorian calendar, for example.

A simple example: /
/ is not just a char, but a date separator that depends on the culture.

Related

DateTime Format - Any System Datetime.now to "DD/MM/YYYY hh:mm:ss"

I have a program that do several things.
Two of them is read a date from a txt and rewrite a date in the same txt.
The read of the date is a regex expression like:
[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-5]{1}[0-9]{1})
The problem is that my regex expression only works in the format
"DD/MM/YYYY hh:mm:ss" and its impossible to make sure my regex expression can match all system datetime formats.
So, I need to make sure my program run's in every system, regardless the system datetime.now.
For that, i thought about format every system datetime.now, at start, to the format mentioned "DD/MM/YYYY hh:mm:ss".
At the moment i have the following code:
Datetime currentDate = DateTime.ParseExact(DateTime.Now.ToString(), "DD/MM/YYYY hh:mm:ss", CultureInfo.InvariantCulture);
However, when running some tests, using a system date in format "D/M/YYYY h:m:s" i get the error:
"String was not recognized as a valid DateTime."
The problem is that if my date, for example, is "9/27/2019 04:26:46"(M/D/YYYY h:m:s) it can't fit in the format i defined.
Any idea?
Thank you in advance!
You need to use the same format string and culture in every place where you convert the DateTime to string as well. In your sample code, you're doing
DateTime.Now.ToString()
This uses the default culture for the thread, and the default format. Unless assigned otherwise, the thread is probably using the local culture info. Instead, you would want to use the same format and the invariant culture:
DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
(note the lowercase "dd". "DD" is not a valid format specifier for date times; these things are case sensitive. Also note the "HH", which gives a 24-hour value, rather than 12-hour)
In practice, just using the invariant culture should be enough for persistence. Cultures already include default datetime formats, so unless you have a specific need to use a different format, why not use the default?
Also note that DateTime doesn't have a format. The format only comes into play when you convert from or to a string. That is the place where you need to ensure the same culture and format is used for both sides of the operation (and that's why for persistence, especially for data shared between different users or computers, you generally want to use the invariant culture).
If you need
to make sure my program run's in every system, regardless the system datetime.now
you can adapt international standard for this, say, ISO 8601.
In order to validate the DateTime, regular expressions like you have are not enough (just imagine leap years), but TryParse does it job:
string source = "2019-09-26T23:45:59";
// Either current culture date and time format or ISO
bool isValid = DateTime.TryParse(
source,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal,
out var _date);
Or if you want to be more restrictive use TryParseExact:
// ISO only
bool isValid = DateTime.TryParseExact(
source,
"s",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal,
out var _date);
If you want to represent DateTime.Now in ISO 8601, add "s" standard format string:
string dateAsString = DateTime.Now.ToString("s");
Alas, you can provide a bunch of formats which are able to cope with any date and time formats; a classical example of ambiguous date is
01/02/03 - 01 Feb 2003 (Russia)
01/02/03 - 02 Jan 2003 (USA)
01/02/03 - 03 Feb 2001 (China)
You can alleviate the problem, while providing several formats:
// Here we try to support 4 formats (note different delimeters)
string[] formats = new string[] {
"s", // try ISO first
"dd'.'MM'.'yyyy HH':'mm':'ss", // if failed try Russian
"MM'/'dd'/'yyyy HH':'mm':'ss", // on error have a look at USA
"yyyy'-'MM'-'dd HH':'mm':'ss", // the last hope is Chinese
};
bool isValid = DateTime.TryParse(
source,
formats,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal,
out var date);

Flexible DateTime parsing on multi culture web application

I have an ASP.NET web application which is multi culture meaning I have en-us, en-ca, fr-ca, etc.
My problem is when I am trying to Parse a date 1/22/2014 using DateTime.Parse and I am using en-us, it will work because the ShortDatePattern of en-us is M/dd/yyyy but if the user is en-ca, the ShortDatePattern is dd/MM/yyyy.
How can I parse the dates considering different cultures? I have tried the following codes:
DateTime.Parse(date);
DateTime.ParseExact(date, ShortDatePattern, Culture);
DateTime.TryParseExact(date, ShortDatePattern, Culture, DateTimeStyles.None, out date);
But still no luck for me.
EDIT
DateTime.Parse throws me an exception, string is not a valid datetime. Same with the DateTime.ParseExact. DateTime.TryParseExact give me a date of 1/1/0001.
If you're absolutely sure of the user's culture - and that they'll actually use that - you could use:
// I assume that Culture is a valid reference to a CultureInfo...
DateTime date = DateTime.Parse(date, Culture);
However, I'd strongly consider providing a calendar control or separate year/text-month/day fields on the page (with validation) so that what you post back to ASP.NET can be a machine-readable, culture-neutral date format, e.g. yyyy-MM-dd. Basically, turn the culture-sensitive representation into a culture-neutral representation as early as you can.
If the user can choose multiple languages within your application, I would think it would be easier to cater for the multiple language choices.
Here is what I did sometime ago:
DateTime dateValue = DateTime.Parse(dateVar.ToString());
string currentCulture = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IeftLanguageTag).ToString();
CultureInfo culture = new CultureInfo(currentCulture);
Console.WriteLine(dateValue.ToString("d", culture));
DateVar being the date value that you want to convert to new culture.
The above code makes use of the System.Windows.Markup namespace
Just change Console.WriteLine your preferred output display.

Why is not the day of the week formated in long date format for all languages?

We have used date.ToString ("D") for formatting the date. But when we globalizat the application to other languages ​​we encounter problems. We expect the day of the week to appear when we use the long date format, but it's not lake so for all languages. According http://www.basicdatepicker.com/samples/cultureinfo.aspx, it is far from all language that print out day a week for the long date format. How should we do to format the date?
If you need an explicit format, specify it like so;
DateTime date = DateTime.Now;
string s = date.ToString("dddd, dd MMMM yyyy");
According to the MSDN doco, the "D" format specifier is affected by the Calendar, which is Culture specific.
You shouldn't make assumptions about string formatting or Date display in particular, when internationalising.

datetime.parseexact returns wrong month

Here is my code:
a.dateFrom = DateTime.ParseExact(x, "dd/mm/yyyy", null);
And x has value of: 08/03/2012
However, a.dateFrom has value of 08/01/2012. Why?
You should use MM as format for month
As ionden notes, you should have a format of
"dd/MM/yyyy"
Currently you're parsing the second part as minutes (as that's what mm means).
See the documentation for custom date and time format strings for more information. I'd also strongly encourage you to consider using the invariant culture for parsing - if you're using a custom format string, that usually means you don't want to treat the input in a culture-sensitive fashion at all.

C# DateTime Parse/convert this format "20120314T130000"

I am using an API that outputs dates in this format 20120314T130000 .
The date value is 13:00 14 March 2012. How can I parse a Date in this format to a .Net DateTime variable in C#?
Also what is this date format known as?
That's ISO 8601. The '-' separators are optional in this format.
You can't parse it with the normal DateTime.Parse method, but you can use ParseExact:
DateTime.ParseExact(date, "yyyyMMdd'T'HHmmss", CultureInfo.InvariantCulture)
If you have a mix of dates, some with separators and some without, you might need to use a regex to extract the relevant information and then construct the DateTime object.

Categories

Resources