473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Nov 17 '05 #1
3 6788
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" <_n**************************@hotmail.com> wrote in message
news:3F**************@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
Nov 17 '05 #2
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" <_n**************************@hotmail.com> wrote in message
news:3F**************@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
Nov 17 '05 #3
>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...
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James | last post by:
I need to import a bunch of data into our database for which there's a single entry each day which occurs at the same time every day in local time - so I need to convert this to UTC taking into...
14
by: ChrisM | last post by:
Could anyone please tell me the difference between these 2 lines: myDate = DateTime.Now; and myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy")); Seeing as later on in the...
4
by: Hans Merkl | last post by:
Does anybody know of a library that can handle strings pf various formats and conver them to a DateTime value? The strings are coming from a webform and I can't restrict the input (it's not my...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
0
by: Kevin Kenny | last post by:
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...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
5
by: js | last post by:
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss". I need to parse the text using System.DateTime.Parse() function with custom format. I got an error using the following code. ...
4
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am having a problem formatting a string when the time is in format hh.mm.ss - used in Europe Parse seems ok when the date uses "/" or "." as seperator but I get an exception when time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.