Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP.NET DateTime.Parse oddness

Reply
Thread Tools

ASP.NET DateTime.Parse oddness

 
 
Kevin Kenny
Guest
Posts: n/a
 
      08-09-2003
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

 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-10-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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


 
Reply With Quote
 
 
 
 
Kevin Kenny
Guest
Posts: n/a
 
      08-10-2003
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" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>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


 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-10-2003
>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...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TRY/CATCH ODDNESS Obi Wan Shinobi Java 6 04-15-2006 03:48 AM
Some oddness with tv shows RichA DVD Video 0 01-21-2005 10:44 PM
float and clear oddness requires border? Uncle Pirate HTML 2 08-12-2004 01:56 AM
XP User Profile Oddness XP Newb Computer Support 2 11-24-2003 06:38 AM
"High Fidelity" oddness Berndt Jobst DVD Video 1 07-05-2003 01:37 PM



Advertisments