Reputation: 135
I have a data file being supplied by a vendor who can not follow directions. We have three date fields that they need to populate Start_date, Process_date, Report_date. We require the format to be mm/dd/yyyy, however since they are sending the reports from different countries we get a hodgpog. dd/mm/yyyy or the correct mm/dd/yyyy. the problem is in the same column of date me get both formats. Does anyone have any idea how i could validate a given value to determine if they used dd/mm/yyyy or the correct mm/dd/yyyy. Assume i am setting a parameter stdt = "givenvalue".
Upvotes: 0
Views: 5984
Reputation: 176
I'm afraid, that with mixed data you are at the mercy of your vendors.
What I would suggest is that you require the data in three separate data fields: day, month, year.
It should be easier for them to supply you with day, month, year instead of them trying to reformat from their local date format to the American format.
This is coming from Spain, where the date/number formats have made my life 'interesting' for many years now.
Upvotes: 2
Reputation: 65
Can you have in the report file one more column (or value) having the country of origin and from there choose the correct format?
string userDate = "6/15/2018", newDate; //fill date from your report
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
if (country.Text == "USA")
{
try
{
DateTime.TryParse(userDate,provider, DateTimeStyles.None,out result);
newDate = result.ToString("MM/dd/yyyy");
}
catch (Exception)
{
//some code
}
}
else // example for LatinAmerica
{
provider = new CultureInfo("es-MX"); //LatinAmerica uses day/month
try
{
DateTime.TryParse(userDate, provider, DateTimeStyles.None, out result); // This converts it to your expected format.
newDate = result.ToString("MM/dd/yyyy");
}
catch (Exception)
{
//some code
}
}
Result for USA = 6/15/2018
Result for Latam = 6/15/2018
Upvotes: 0
Reputation: 5131
You shouldn't require vendors around the world to use some arbitrary format of text.
No vendor should be expected to do weird logic like:
var today = DateTime.Now;
message.ReportDate = $"{today.Month}/{today.Day}/{today.Year}";
all over their code, it's bug prone, and weird. And yes, there are ways any developer could do the same thing but prettier... But for illustration I'll leave it.
Instead, a better option would be to allow the local culture info (perhaps as an additional field)
public class Report
{
public string Name {get; set;}
public string ReportDate { get; set; }
public string ISOLanguageCode { get; set; }
}
If the language code isn't set, then assume the ISO 8601 format of YYYYMMDD and at least a lot of countries will work by default:
Console.WriteLine(DateTime.Parse("2020.07.20"));
Console.WriteLine(DateTime.Parse("2020-07-20"));
Console.WriteLine(DateTime.Parse("2020/07/20"));
But when the ISO language code is specified: You can use any format:
DateTime dt;
if (!IsNullOrEmpty(report.ISOLanguageCode))
{
System.Globalization.CultureInfo cultureinfo =
System.Globalization.CultureInfo("report.ISOLanguageCode");
dt = DateTime.Parse(report.ReportDate, cultureinfo);
}
else
{
dt = DateTime.Parse(report.ReportDate);
}
This means for the en-US format you could still do:
var report = new Report()
{
Name = "July report 20",
Date = "07/20/2020",
ISOLanguageCode = "en-US"
};
and every thing should be much more stable.
And assuming your data layer needs the current format for posterity: Just add it in the BL/DL as appropriate:
dbContext.Add(new Report()
{
//... Most the other properties
ReportDate = report.ToString("dd/MM/yyyy"),
};
Upvotes: 1
Reputation: 4279
You can check if first 2 digits is greater than 12 the format is probably DD/MM/YYYY or if combination 3rd or 4th digit is greater than 12 the format is probably MM/DD/YYYY. But this conditions does only applies if the date contains a value > 12. So, using this method will not give you 100% correct results.
The other method might be checking locale defaults. You can check which country uses what format and parse the date using that format depending on report country / locale.
Or you might just ask them to supply the date on specific format only.
Upvotes: 1