locked
Correct way to check date formats RRS feed

  • Question

  • User1486439998 posted

    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.

                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

    George

    Thursday, September 25, 2014 7:22 AM

Answers

  • User-434868552 posted

    @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:

    void Main()
    {
        Console.WriteLine (ConvertUkDateToUsDateFormat.ConvertToUsFormat("31/12/10"));
        Console.WriteLine (ConvertUkDateToUsDateFormat.ConvertToUsFormat("12/31/11"));
    }

    George, you're expecting something like this:

    12/31/2010
    12/31/2011

    actual output on my system:

    12/31/2010
    2011-12-31

    Your code:

    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.

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 25, 2014 7:45 AM
  • User-434868552 posted

    @George Phill...   

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 25, 2014 2:42 PM

All replies

  • User-434868552 posted

    @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:

    void Main()
    {
        Console.WriteLine (ConvertUkDateToUsDateFormat.ConvertToUsFormat("31/12/10"));
        Console.WriteLine (ConvertUkDateToUsDateFormat.ConvertToUsFormat("12/31/11"));
    }

    George, you're expecting something like this:

    12/31/2010
    12/31/2011

    actual output on my system:

    12/31/2010
    2011-12-31

    Your code:

    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.

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 25, 2014 7:45 AM
  • User1486439998 posted

    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

    Thursday, September 25, 2014 1:07 PM
  • User-434868552 posted

    @George Phill...   

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 25, 2014 2:42 PM