Date format causing great problems!

  • Thread starter Thread starter Jozef Jarosciak
  • Start date Start date
J

Jozef Jarosciak

I have an application which imports fields from external source where
date format of one the collumns is English (United States): M/d/yyyy.
So July 1, 2005 equals to: 7/1/05
I am using this date everywhere in the application.

Problem is when someones is lets say in Portugal with the regional
settings for date set to DD-MM-YYYY.

I am going crazy to figure out how to make everyone to use M/d/yyyy
scheme without changing their regional settings, as lots of people
simply can't do that.

Can someone point me to what I could do, or post some code on how to go
around this annoying problem?

It would be very much appreciated.

Joe
 
Jozef Jarosciak said:
I have an application which imports fields from external source where
date format of one the collumns is English (United States): M/d/yyyy.
So July 1, 2005 equals to: 7/1/05
I am using this date everywhere in the application.

Problem is when someones is lets say in Portugal with the regional
settings for date set to DD-MM-YYYY.

I am going crazy to figure out how to make everyone to use M/d/yyyy
scheme without changing their regional settings, as lots of people
simply can't do that.

Can someone point me to what I could do, or post some code on how to go
around this annoying problem?

It would be very much appreciated.

Joe

One way to is to detect if the date coming in is non US and then do some
type of transforming on it. You mention external source. Is there
something coming with the data to signify its country or at least that it is
non US? What is the data type they are sending back?

Use some of the DateTime methods to return a date against your imported
date. Then you can go further to pick it apart by hours, minutes, month,
year if you need to. From that you can construct the date format you need
or possibly just format off of the returned date if DateTime allows.

Brett
 
Hi Brett,
I need some one liner, code which would set the whole program (form) to
"M/d/yyyy" so whenever I do have to deal with date it would forget
about my users DD-MM-YYYY format and assume they all have "M/d/yyyy".
Is there any way to ignore regional settings and its date format?
Joe
 
Jozef Jarosciak said:
Hi Brett,
I need some one liner, code which would set the whole program (form) to
"M/d/yyyy" so whenever I do have to deal with date it would forget
about my users DD-MM-YYYY format and assume they all have "M/d/yyyy".
Is there any way to ignore regional settings and its date format?
Joe

Have you tried
DateTime.Parse(TextBox1.Text).ToString("M/d/yyyy");

Are users editing this data in the form or does it appear from you import as
read only to them?

Brett
 
It is usually best to let users enter dates (and numbers) in the format they
are most familiar with. Trying to force them to do otherwise will only lead
to data entry errors.

The date format only matters when your application interacts with the
'outside world', i.e. your users or the database. Within the application,
the DateTime variables are independent of their value's textual
representation.

In your external interactions, use the appropriate CultureInfo object, which
you can then pass as the IFormatProvider argument to the DateTime's Parse or
ToString methods as appropriate.

The current user's CultureInfo is available through the CurrentUICulture
property of CultureInfo. It sounds like your database uses a specific
culture, for example: "new CultureInfo("EN-US")"

For more information, including code samples, look up these class and
interface names in the .NET framework documentation.

Jeffrey Sax
Extreme Optimization
http://www.extremeoptimization.com
 
One example of what Jeffrey is talking about is:

IFormatProvider culture = new CultureInfo("en-US", true);
string DateTimeEnValue = DateTime.Now.ToString();
string myShortEnglishDate = DateTimeEnValue .ToShortDateString();

Brett
 
I am sorry, but this is a spanish village for me.
Can someone show me on simple example how I force my whole application
to use en-US format?
I don't use threads in my program, so I can't apply cultureinfo to
thread.

Imagine this.
Simple case scenario:
I ask for datetime.now and it gives me: 7/1/2005
But my user gets: 1/7/2005
So when I further ask for day of the week, which I want to highlight,
it is all screwed up, as it thinks it's 7th of January as opose to
correct 1 of July.

I need a way to force the whole program to think user is in USA with US
regional settings for date.

Is there a way to do this?
Joe
 
This is a fix I found in vb.net and worked like a charm for me.


Dim ci As New System.Globalization.CultureInfo("en-US", False)
Dim newCi As System.Globalization.CultureInfo =
CType(ci.Clone(), System.Globalization.CultureInfo)
newCi.DateTimeFormat.AMDesignator = "AM"
newCi.DateTimeFormat.PMDesignator = "PM"
newCi.DateTimeFormat.ShortDatePattern = "M/d/yyyy"
Thread.CurrentThread.CurrentCulture = newCi

Just wanted to post it and share it with other. Now my whole
application is switched to en-US format, no matter what country is my
user from.
Joe
 
Can someone show me on simple example how I force my whole application
to use en-US format?
Sorry, but what all are trying to explaint is that this is really
a bad thing to do.
 
Back
Top