ASP.NET DateTime.Parse oddness

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Kenny

    #1

    ASP.NET DateTime.Parse oddness

    Dear All,

    I have a date time validation method thus:

    public static bool IsDate(string date, System.IFormatP rovider provider) {
    try {
    DateTime.Parse( date, provider)
    return true;
    } catch (System.FormatE xception)
    return false;
    }
    }

    This works a treat from a console app:
    e.g. bool b = Utils.IsDate("2 1/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. ShortDatePatter n, 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
    ShortDatePatter n 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]

    #2
    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.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Sess ionState;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.H tmlControls;
    using System.Threadin g;
    using System.Globaliz ation ;


    namespace p733workcs1
    {
    /// <summary>
    /// Summary description for dtculture.
    /// </summary>
    public class dtculture : System.Web.UI.P age
    {
    protected System.Web.UI.W ebControls.Labe l Label1;

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    bool b = IsDate("2/21/2003", new CultureInfo("en-GB"));
    Label1.Text=b.T oString() ;
    }

    public static bool IsDate(string date, System.IFormatP rovider provider)
    {
    System.Threadin g.Thread.Curren tThread.Current UICulture = new
    CultureInfo("en-GB");
    System.Threadin g.Thread.Curren tThread.Current Culture = new
    CultureInfo("en-GB");

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

    catch (System.FormatE xception e)
    {
    return false;

    }

    }

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

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

    }
    #endregion
    }
    }

    "Kevin Kenny" <_no_spam_today _kevin_e_kenny@ hotmail.com> wrote in message
    news:3F352D2D.3 090708@hotmail. com...
    Dear All,

    I have a date time validation method thus:

    public static bool IsDate(string date, System.IFormatP rovider provider) {
    try {
    DateTime.Parse( date, provider)
    return true;
    } catch (System.FormatE xception)
    return false;
    }
    }

    This works a treat from a console app:
    e.g. bool b = Utils.IsDate("2 1/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. ShortDatePatter n, 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
    ShortDatePatter n 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


    Comment

    • Kevin Kenny

      #3
      Re: ASP.NET DateTime.Parse oddness

      Ken Cox [Microsoft MVP] wrote:
      [color=blue]
      >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.Collecti ons;
      >using System.Componen tModel;
      >using System.Data;
      >using System.Drawing;
      >using System.Web;
      >using System.Web.Sess ionState;
      >using System.Web.UI;
      >using System.Web.UI.W ebControls;
      >using System.Web.UI.H tmlControls;
      >using System.Threadin g;
      >using System.Globaliz ation ;
      >
      >
      >namespace p733workcs1
      >{
      > /// <summary>
      > /// Summary description for dtculture.
      > /// </summary>
      > public class dtculture : System.Web.UI.P age
      > {
      > protected System.Web.UI.W ebControls.Labe l Label1;
      >
      > private void Page_Load(objec t sender, System.EventArg s e)
      > {
      > bool b = IsDate("2/21/2003", new CultureInfo("en-GB"));
      > Label1.Text=b.T oString() ;
      > }
      >
      > public static bool IsDate(string date, System.IFormatP rovider provider)
      > {
      > System.Threadin g.Thread.Curren tThread.Current UICulture = new
      >CultureInfo("e n-GB");
      > System.Threadin g.Thread.Curren tThread.Current Culture = new
      >CultureInfo("e n-GB");
      >
      > try
      > {
      > DateTime.Parse( date, provider);
      > return true;
      > }
      >
      > catch (System.FormatE xception e)
      > {
      > return false;
      >
      > }
      >
      > }
      >
      > #region Web Form Designer generated code
      > override protected void OnInit(EventArg s e)
      > {
      > //
      > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      > //
      > InitializeCompo nent();
      > base.OnInit(e);
      > }
      >
      > /// <summary>
      > /// Required method for Designer support - do not modify
      > /// the contents of this method with the code editor.
      > /// </summary>
      > private void InitializeCompo nent()
      > {
      > this.Load += new System.EventHan dler(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.IFormatP rovider provider) {
      > try {
      > DateTime.Parse( date, provider)
      > return true;
      > } catch (System.FormatE xception)
      > return false;
      > }
      >}
      >
      >This works a treat from a console app:
      > e.g. bool b = Utils.IsDate("2 1/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 .ShortDatePatte rn, 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
      >ShortDatePatte rn 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
      >
      >
      >
      >[/color]
      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.ShortD atePattern (M/d/YYYY instead of dd/MM/yyyy). Explicitly
      assigning the en-GB CultureInfo object to
      Thread.CurrentT hread.CurrentCu lture &
      Thread.CurrentT hread.CurrentUI Culture 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 ShortDatePatter n in code but I shouldn't have to do this. And
      additionally this all works just dandy from a console app.

      Regards
      Kevin


      Comment

      • Ken Cox [Microsoft MVP]

        #4
        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...


        Comment

        Working...