ASP.NET DateTime.Parse oddness

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default ASP.NET DateTime.Parse oddness

    Dear All,

    I have a date time validation method thus:

    public static bool IsDate(string date, System.IFormatProvider provider) {
    try {
    DateTime.Parse(date, provider)
    return true;
    } catch (System.FormatException)
    return false;
    }
    }

    This works a treat from a console app:
    e.g. bool b = Utils.IsDate("21/2/2003", new CultureInfo("en-GB"));

    However DateTime.Parse throws an exception if the method is called from
    ASP.NET.

    Upon further inspection of the CultureInfo object's
    DateTimeFormat.ShortDatePattern, it reveals itself as "dd/MM/yyyy" when
    called from the console app but is set to "M/d/yyyy" when called from an
    ASP.NET application.

    Anyone got a clue as to whats happening here. Setting the
    ShortDatePattern to "dd/MM/yyyy" solved the problem but it is not very
    satisfying when supposedly the culture info object ought to be correct
    for en-GB.

    The environment is XP, VS.NET2002, FW 1.0.3705.288.

    Thanks
    Kevin

    Kevin Kenny Guest

  2. Similar Questions and Discussions

    1. Accordion oddness
      With the below accordion, without making Detail tab visible if I say detailtext.text="zzz" I get TypeError: Error #1009: null has no...
    2. Lexmark C910 oddness
      When printing multiple copies of our PDF to our Lexmark C910, only the first page gets text. The subsequent pages only print graphics. The odd thing...
    3. Cross-Platform Code Oddness
      I'm working my way through a Flash book, and there's a little bit of code in it that explains how the "_rotation" property works. It's attached to a...
    4. [PHP-DEV] MZ-S oddness
      Last weekend I took a roll of Kodak T-Max 100 with the MZ-S When developed, the film is hopelessly under exposed, as if the flashes had never been...
    5. Rubygarden oddness
      All, It appears that "herbless" is posting junk to RubyGarden. Can we get this removed? If "herbless" is following this list, please explain...
  3. #2

    Default Re: ASP.NET DateTime.Parse oddness

    Hi Kevin,

    In ASP.NET, you're going to have to make sure that the thread knows about the
    culture. It may be that the box is set up as US and not GB.

    Try this the code below?

    Ken
    MVP [ASP.NET]



    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Threading;
    using System.Globalization ;


    namespace p733workcs1
    {
    /// <summary>
    /// Summary description for dtculture.
    /// </summary>
    public class dtculture : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    bool b = IsDate("2/21/2003", new CultureInfo("en-GB"));
    Label1.Text=b.ToString() ;
    }

    public static bool IsDate(string date, System.IFormatProvider provider)
    {
    System.Threading.Thread.CurrentThread.CurrentUICul ture = new
    CultureInfo("en-GB");
    System.Threading.Thread.CurrentThread.CurrentCultu re = new
    CultureInfo("en-GB");

    try
    {
    DateTime.Parse(date, provider);
    return true;
    }

    catch (System.FormatException e)
    {
    return false;

    }

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
    }

    "Kevin Kenny" <_no_spam_today_kevin_e_kenny@hotmail.com> wrote in message
    news:3F352D2D.3090708@hotmail.com...
    Dear All,

    I have a date time validation method thus:

    public static bool IsDate(string date, System.IFormatProvider provider) {
    try {
    DateTime.Parse(date, provider)
    return true;
    } catch (System.FormatException)
    return false;
    }
    }

    This works a treat from a console app:
    e.g. bool b = Utils.IsDate("21/2/2003", new CultureInfo("en-GB"));

    However DateTime.Parse throws an exception if the method is called from
    ASP.NET.

    Upon further inspection of the CultureInfo object's
    DateTimeFormat.ShortDatePattern, it reveals itself as "dd/MM/yyyy" when
    called from the console app but is set to "M/d/yyyy" when called from an
    ASP.NET application.

    Anyone got a clue as to whats happening here. Setting the
    ShortDatePattern to "dd/MM/yyyy" solved the problem but it is not very
    satisfying when supposedly the culture info object ought to be correct
    for en-GB.

    The environment is XP, VS.NET2002, FW 1.0.3705.288.

    Thanks
    Kevin


    Ken Cox [Microsoft MVP] Guest

  4. #3

    Default Re: ASP.NET DateTime.Parse oddness

    Ken Cox [Microsoft MVP] wrote:
    >Hi Kevin,
    >
    >In ASP.NET, you're going to have to make sure that the thread knows about the
    >culture. It may be that the box is set up as US and not GB.
    >
    >Try this the code below?
    >
    >Ken
    >MVP [ASP.NET]
    >
    >
    >
    >using System;
    >using System.Collections;
    >using System.ComponentModel;
    >using System.Data;
    >using System.Drawing;
    >using System.Web;
    >using System.Web.SessionState;
    >using System.Web.UI;
    >using System.Web.UI.WebControls;
    >using System.Web.UI.HtmlControls;
    >using System.Threading;
    >using System.Globalization ;
    >
    >
    >namespace p733workcs1
    >{
    > /// <summary>
    > /// Summary description for dtculture.
    > /// </summary>
    > public class dtculture : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.Label Label1;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > bool b = IsDate("2/21/2003", new CultureInfo("en-GB"));
    > Label1.Text=b.ToString() ;
    > }
    >
    > public static bool IsDate(string date, System.IFormatProvider provider)
    > {
    > System.Threading.Thread.CurrentThread.CurrentUICul ture = new
    >CultureInfo("en-GB");
    > System.Threading.Thread.CurrentThread.CurrentCultu re = new
    >CultureInfo("en-GB");
    >
    > try
    > {
    > DateTime.Parse(date, provider);
    > return true;
    > }
    >
    > catch (System.FormatException e)
    > {
    > return false;
    >
    > }
    >
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    > }
    >}
    >
    >"Kevin Kenny" <_no_spam_today_kevin_e_kenny@hotmail.com> wrote in message
    >news:3F352D2D.3090708@hotmail.com...
    >Dear All,
    >
    >I have a date time validation method thus:
    >
    >public static bool IsDate(string date, System.IFormatProvider provider) {
    > try {
    > DateTime.Parse(date, provider)
    > return true;
    > } catch (System.FormatException)
    > return false;
    > }
    >}
    >
    >This works a treat from a console app:
    > e.g. bool b = Utils.IsDate("21/2/2003", new CultureInfo("en-GB"));
    >
    >However DateTime.Parse throws an exception if the method is called from
    >ASP.NET.
    >
    >Upon further inspection of the CultureInfo object's
    >DateTimeFormat.ShortDatePattern, it reveals itself as "dd/MM/yyyy" when
    >called from the console app but is set to "M/d/yyyy" when called from an
    >ASP.NET application.
    >
    >Anyone got a clue as to whats happening here. Setting the
    >ShortDatePattern to "dd/MM/yyyy" solved the problem but it is not very
    >satisfying when supposedly the culture info object ought to be correct
    >for en-GB.
    >
    >The environment is XP, VS.NET2002, FW 1.0.3705.288.
    >
    >Thanks
    >Kevin
    >
    >
    >
    >
    Hi Ken,

    Thanks for the input.

    The box is configured for UK locales throughout. The problem is that the
    en-GB culture info objects are being instantiated with the wrong
    DateTime.ShortDatePattern (M/d/YYYY instead of dd/MM/yyyy). Explicitly
    assigning the en-GB CultureInfo object to
    Thread.CurrentThread.CurrentCulture &
    Thread.CurrentThread.CurrentUICulture doesn't solve the problem because
    when they are instantiated they are incorrect and all you are doing is
    assigning an already wrong CultureInfo object.

    As i said in my last post the only way round this is to explicitly set
    the ShortDatePattern in code but I shouldn't have to do this. And
    additionally this all works just dandy from a console app.

    Regards
    Kevin


    Kevin Kenny Guest

  5. #4

    Default Re: ASP.NET DateTime.Parse oddness

    >The box is configured for UK locales throughout.

    Just out of interest, does everything look correct when you go to the Regional
    and Language Options applet > Customize ? Short Date?

    Is it possible that there's something in the machine.config or web.config that
    is changing the short date format?

    Just tossing out ideas here...


    Ken Cox [Microsoft MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139