Can anyone let me know if this is the correct way to check a date is in the correct format.
I require the date format to be UK date, but if for some reason the user manages to bypass my client validation and enter a date in US format I can still handle it and accept it.
Any other format will fail.
var convertArrivalDateToUsFormat = ConvertUkDateToUsDateFormat.ConvertToUsFormat(m.ArrivalDate.ToString(CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat));
public static class ConvertUkDateToUsDateFormat
{
public static string ConvertToUsFormat(string dateFormat)
{
try
{
var gb = new CultureInfo("en-GB");
var us = new CultureInfo("en-US");
var convertUkFormat = DateTime.Parse(dateFormat, gb);
string usFormat = convertUkFormat.ToString("d", us);
return usFormat;
}
catch (Exception)
{
try
{
var us = new CultureInfo("en-US");
var usFormat = DateTime.Parse(dateFormat, us);
return usFormat.ToString("d");
}
catch (Exception)
{
return "-99";
}
}
}
}
The reason date needs to be UK is mainly UK visitors, but external source requires date to be US format
public static class ConvertUkDateToUsDateFormat
{
public static string ConvertToUsFormat(string dateFormat)
{
try
{
var gb = new System.Globalization.CultureInfo("en-GB");
var us = new System.Globalization.CultureInfo("en-US");
var convertUkFormat = DateTime.Parse(dateFormat, gb);
string usFormat = convertUkFormat.ToString("d", us);
return usFormat;
}
catch (Exception)
{
try
{
var us = new System.Globalization.CultureInfo("en-US");
var usFormat = DateTime.Parse(dateFormat, us);
return usFormat.ToString("d");
}
catch (Exception)
{
return "-99";
}
}
}
}
George, if you want to have fail-safe string results from DateTime values, you need to be more explicit in the construction of your end results.
the link above is about "How to get the exact U.S. standard time format from the DateTime.Now string value", however the principle is identical whether one wants only date, only time, date with time, et cetera.
N.B.: imho, the best format is always this: yyyy-mm-dd, it's the
least likely to be confused because i've never seen anyone write yyyy-dd-mm.
(a long time ago, a television murder mystery's plot was based on the confusion of the detectives regarding a date the dying victim had written in the dirt; when they realized the format the victim had used, they solved the crime)
Most of the english speaking world uses day/month ... the U.S. uses the ancient month/day.
end edit.
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
Thanks for the reply, as the home page has a datepicker, I think the best option would be to split the string on / and then put it back together in the format I require.
the best option would be to split the string on / and then put it back together in the format I require.
if you're talking about after your end user has made the selection, the question to ask yourself is can you guarantee that the formats provided by the .NET Framework will always meet your needs; given that those formats are often dependent upon the end user's
system settings, it is likely that you can not make that guarantee ... for that reason, it is best to use output from your "datepicker" that presumably you can trust AFAIK, push that output with DateTime.TryParse into a DateTime struct, and then extract from
the DateTime value the separate parts from which you can put together the string that you require using the type of strategy shown in my examples.
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
Member
9 Points
70 Posts
Correct way to check date formats
Sep 25, 2014 07:22 AM|George Phillipson|LINK
Hi
Can anyone let me know if this is the correct way to check a date is in the correct format.
I require the date format to be UK date, but if for some reason the user manages to bypass my client validation and enter a date in US format I can still handle it and accept it.
Any other format will fail.
The reason date needs to be UK is mainly UK visitors, but external source requires date to be US format
George
Star
14297 Points
5797 Posts
Re: Correct way to check date formats
Sep 25, 2014 07:45 AM|gerrylowry|LINK
@George Phill...
if you are going to use DateTime, you will always be up against you end user's system settings, using your code as a starting point:
George, you're expecting something like this:
actual output on my system:
Your code:
George, if you want to have fail-safe string results from DateTime values, you need to be more explicit in the construction of your end results.
as an example, see http://forums.asp.net/post/5773075.aspx
edit:
the link above is about "How to get the exact U.S. standard time format from the DateTime.Now string value", however the principle is identical whether one wants only date, only time, date with time, et cetera.
this example deals with date: http://forums.asp.net/post/5770221.aspx
N.B.: imho, the best format is always this: yyyy-mm-dd, it's the least likely to be confused because i've never seen anyone write yyyy-dd-mm.
(a long time ago, a television murder mystery's plot was based on the confusion of the detectives regarding a date the dying victim had written in the dirt; when they realized the format the victim had used, they solved the crime)
Most of the english speaking world uses day/month ... the U.S. uses the ancient month/day.
end edit.
Member
9 Points
70 Posts
Re: Correct way to check date formats
Sep 25, 2014 01:07 PM|George Phillipson|LINK
Hi gerrylowry
Thanks for the reply, as the home page has a datepicker, I think the best option would be to split the string on / and then put it back together in the format I require.
Regards
George
Star
14297 Points
5797 Posts
Re: Correct way to check date formats
Sep 25, 2014 02:42 PM|gerrylowry|LINK
@George Phill...
if you're talking about after your end user has made the selection, the question to ask yourself is can you guarantee that the formats provided by the .NET Framework will always meet your needs; given that those formats are often dependent upon the end user's system settings, it is likely that you can not make that guarantee ... for that reason, it is best to use output from your "datepicker" that presumably you can trust AFAIK, push that output with DateTime.TryParse into a DateTime struct, and then extract from the DateTime value the separate parts from which you can put together the string that you require using the type of strategy shown in my examples.