473,586 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making my DateTime object think its American :-)

Hi all,

I'm having a bit of a problem working with dates. My computer is british,
but I'm developing an american application so I therefore need to use american
dates. But I can't get my DateTime object to "act" american.

Let me give an example:

In order to take in an american date, I tell it which culture the date will
be formatted according to:

if(!(DateTime.T ryParse(txtAvai lableFrom.Text, new CultureInfo("en-US"), DateTimeStyles. None,
out arrivalDate))){

This all works fine and the DateTime interprets the textbox contents correctly
as an American date. The problem comes later when I try and use the date.

For example, when I try and do date.ToString() , it outputs it in the British
format. Now this one isnt a problem because I know how to force it to output
the american format. (Though it is inconvienient - I'd like the DateTime
object to remember or know somehow that its got an American date and output
accordingly)

The real showstopper for me though is when it comes to passing the date to
the database.

I want all the dates passed to the database to be in the American format.
Unfortunately, when I pass the DateTime object as a parameter to my Stored
Procedure the date is getting stored in the british format.

It's like the DateTime intrinsically knows that its in Britain, even though
I forced it to accept an American date.

Can anyone tell me if there's anyway I can force it to act like an American
date both when outputing and more importantly when I pass it to the database?
I need it to forget that its in Britain and get it to start acting American
stylee pronto! :-)

Thanks to anyone who can help. This thing has been driving me crazy for ages.
It's getting to crunch time soon and I'm begining to get worried :-(

Thanks again

Simon
Feb 15 '06 #1
9 1725
First, if you're passing it into a database DateTime field, you're not actually
passing it in British format, your table view is likely just outputting that
datetime in your current culture.

Now you say you're developing an American application. Will it run on an
American machine or a British machine? By default, or when you pass CultureInfo.Cur rentCulture,
it will use the culture of that machine. So if you use CurrentCulture, it
will use British on your machine and if you ship it to the US, it will use
US formats there. This is ideally how a culture-aware application should
act.

To have the current thread default to a specific culture, use the Application.Cur rentCulture
property.

Ideally, what you should be doing is on ALL ToString and parse calls, pass
CultureInfo.Cur rentCulture where the information is displayed or taken from
the user. Pass CultureInfo.Inv ariantCulture when you are storing the information
(database, file, etc). This ensures your application will always display
in the correct culture for the user, and your data will be portable accross
cultures. It's a very small bit of extra typing, but very important when
writing applications that will be used in a different culture.

Just remember when mixing cultures, be careful of how your data is persisted.

Hi all,

I'm having a bit of a problem working with dates. My computer is
british, but I'm developing an american application so I therefore
need to use american dates. But I can't get my DateTime object to
"act" american.

Let me give an example:

In order to take in an american date, I tell it which culture the date
will be formatted according to:

if(!(DateTime.T ryParse(txtAvai lableFrom.Text, new
CultureInfo("en-US"), DateTimeStyles. None, out arrivalDate))){

This all works fine and the DateTime interprets the textbox contents
correctly as an American date. The problem comes later when I try and
use the date.

For example, when I try and do date.ToString() , it outputs it in the
British format. Now this one isnt a problem because I know how to
force it to output the american format. (Though it is inconvienient -
I'd like the DateTime object to remember or know somehow that its got
an American date and output accordingly)

The real showstopper for me though is when it comes to passing the
date to the database.

I want all the dates passed to the database to be in the American
format. Unfortunately, when I pass the DateTime object as a parameter
to my Stored Procedure the date is getting stored in the british
format.

It's like the DateTime intrinsically knows that its in Britain, even
though I forced it to accept an American date.

Can anyone tell me if there's anyway I can force it to act like an
American date both when outputing and more importantly when I pass it
to the database? I need it to forget that its in Britain and get it to
start acting American stylee pronto! :-)

Thanks to anyone who can help. This thing has been driving me crazy
for ages. It's getting to crunch time soon and I'm begining to get
worried :-(

Thanks again

Simon

Feb 15 '06 #2
Stupid suggestion, but wouldn't it be easier, for the duration of the
development, to go through Control Panel and change your regional settings
to English(United States).

Steve

"Simon Harvey" <no******@hotma il.com> wrote in message
news:7c******** *************** ***@news.micros oft.com...
Hi all,

I'm having a bit of a problem working with dates. My computer is british,
but I'm developing an american application so I therefore need to use
american dates. But I can't get my DateTime object to "act" american.
Let me give an example:

In order to take in an american date, I tell it which culture the date
will be formatted according to:

if(!(DateTime.T ryParse(txtAvai lableFrom.Text, new CultureInfo("en-US"),
DateTimeStyles. None, out arrivalDate))){

This all works fine and the DateTime interprets the textbox contents
correctly as an American date. The problem comes later when I try and use
the date.

For example, when I try and do date.ToString() , it outputs it in the
British format. Now this one isnt a problem because I know how to force it
to output the american format. (Though it is inconvienient - I'd like the
DateTime object to remember or know somehow that its got an American date
and output accordingly)

The real showstopper for me though is when it comes to passing the date to
the database.

I want all the dates passed to the database to be in the American format.
Unfortunately, when I pass the DateTime object as a parameter to my Stored
Procedure the date is getting stored in the british format.

It's like the DateTime intrinsically knows that its in Britain, even
though I forced it to accept an American date.
Can anyone tell me if there's anyway I can force it to act like an
American date both when outputing and more importantly when I pass it to
the database? I need it to forget that its in Britain and get it to start
acting American stylee pronto! :-)

Thanks to anyone who can help. This thing has been driving me crazy for
ages. It's getting to crunch time soon and I'm begining to get worried :-(

Thanks again

Simon

Feb 15 '06 #3
Try this stuff in a web.config file...

<!--2/10/2006 10:52:49 AM -->
<globalizatio n culture="auto" uiCulture="auto " /> // on my "American"
machine

<!--2/10/2006 10:52:49 AM-->
<globalizatio n culture="en-US" uiCulture="en-US" />

<!--10/02/2006 10:55:56-->
<globalizatio n culture="en-GB" uiCulture="en-GB" />

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee. com
URL http://clintongallagher.metromilwaukee.com/

"Simon Harvey" <no******@hotma il.com> wrote in message
news:7c******** *************** ***@news.micros oft.com...
Hi all,

I'm having a bit of a problem working with dates. My computer is british,
but I'm developing an american application so I therefore need to use
american dates. But I can't get my DateTime object to "act" american.
Let me give an example:

In order to take in an american date, I tell it which culture the date
will be formatted according to:

if(!(DateTime.T ryParse(txtAvai lableFrom.Text, new CultureInfo("en-US"),
DateTimeStyles. None, out arrivalDate))){

This all works fine and the DateTime interprets the textbox contents
correctly as an American date. The problem comes later when I try and use
the date.

For example, when I try and do date.ToString() , it outputs it in the
British format. Now this one isnt a problem because I know how to force it
to output the american format. (Though it is inconvienient - I'd like the
DateTime object to remember or know somehow that its got an American date
and output accordingly)

The real showstopper for me though is when it comes to passing the date to
the database.

I want all the dates passed to the database to be in the American format.
Unfortunately, when I pass the DateTime object as a parameter to my Stored
Procedure the date is getting stored in the british format.

It's like the DateTime intrinsically knows that its in Britain, even
though I forced it to accept an American date.
Can anyone tell me if there's anyway I can force it to act like an
American date both when outputing and more importantly when I pass it to
the database? I need it to forget that its in Britain and get it to start
acting American stylee pronto! :-)

Thanks to anyone who can help. This thing has been driving me crazy for
ages. It's getting to crunch time soon and I'm begining to get worried :-(

Thanks again

Simon

Feb 15 '06 #4
I'm with Steve on this one. Change your PC setting in control panel for
the duration of your build. That would only apply if the host machine
is going to be in the US. Ive read about people have problems changing
CultureInfo.

Jeremy
http://blackstaronline.net/hgtit

Feb 15 '06 #5
If you blokes would learn to spell properly and stop having John Cleese
attack us with Declarations of Revocation
( http://www.stephaniemiller.com/decla...revocation.htm )

you probably would not need to post questions like this. :-)
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Simon Harvey" wrote:
Hi all,

I'm having a bit of a problem working with dates. My computer is british,
but I'm developing an american application so I therefore need to use american
dates. But I can't get my DateTime object to "act" american.

Let me give an example:

In order to take in an american date, I tell it which culture the date will
be formatted according to:

if(!(DateTime.T ryParse(txtAvai lableFrom.Text, new CultureInfo("en-US"), DateTimeStyles. None,
out arrivalDate))){

This all works fine and the DateTime interprets the textbox contents correctly
as an American date. The problem comes later when I try and use the date.

For example, when I try and do date.ToString() , it outputs it in the British
format. Now this one isnt a problem because I know how to force it to output
the american format. (Though it is inconvienient - I'd like the DateTime
object to remember or know somehow that its got an American date and output
accordingly)

The real showstopper for me though is when it comes to passing the date to
the database.

I want all the dates passed to the database to be in the American format.
Unfortunately, when I pass the DateTime object as a parameter to my Stored
Procedure the date is getting stored in the british format.

It's like the DateTime intrinsically knows that its in Britain, even though
I forced it to accept an American date.

Can anyone tell me if there's anyway I can force it to act like an American
date both when outputing and more importantly when I pass it to the database?
I need it to forget that its in Britain and get it to start acting American
stylee pronto! :-)

Thanks to anyone who can help. This thing has been driving me crazy for ages.
It's getting to crunch time soon and I'm begining to get worried :-(

Thanks again

Simon

Feb 15 '06 #6
I've had this problem before, too. I vaguely recall solving it by
passing the date to SQL server as a string representing a universal
date object, e.g.

sqlParam.Value = String.Format(" {0:yyyy-MM-dd}", DateTime.Now);

It was a long time ago, so it may need some tweaking, but hopefully
this gives you something to start with.

Feb 15 '06 #7
A fantastic link - thank you. That has put a smile on my day ;-p

One that *really* grated on me: the Steam (Valve) installer asks you to
select a language, and lists e.g. French with the French flag, German with
the German flag, and (at the top) English with the stars and stripes. Much
gnashing of teeth over here in the UK.

<g>

Marc
Feb 15 '06 #8
Hehe. Obviously, they have got the flags backwards. We Yanks are all simply
transplanted British Subjects, as we so often forget.
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Marc Gravell" wrote:
A fantastic link - thank you. That has put a smile on my day ;-p

One that *really* grated on me: the Steam (Valve) installer asks you to
select a language, and lists e.g. French with the French flag, German with
the German flag, and (at the top) English with the stars and stripes. Much
gnashing of teeth over here in the UK.

<g>

Marc

Feb 15 '06 #9
Try this

System.Threadin g.Thread.Curren tThread.Current Culture = new
System.Globaliz ation.CultureIn fo("en-US");

or ....

System.Threadin g.Thread.Curren tThread.Current Culture = new
System.Globaliz ation.CultureIn fo("en-GB");

This will set the date time control to work in American/British
formating without screwing with your control panel settings

Feb 16 '06 #10

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

Similar topics

1
3235
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python...
4
2518
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it should be used? I can make a datetime easily enough
0
5121
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that accepts messages from .NET clients. The web method call includes an object as one of it's parameters - this reflected object has been given a...
5
1992
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second); I keep getting the below error: Object reference not set to an instance of an object. I don't know what the problem...
11
7209
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 only that I have to listen to them because they know it better. They told also that in a business situation it is better to use...
0
1327
by: Keith Jackson | last post by:
I have, over the last week, been attempting to play with the ObjectDataSource binding an ASP.NET 2.0 UI through to a Data Access Assembly. Selection of data using the new GridView control worked flawlessly but, on attempting any kind of data writing, the system produced the following error... "The DateModified field could not be...
9
2044
by: Simon Harvey | last post by:
Hi all, I'm having a bit of a problem working with dates. My computer is british, but I'm developing an american application so I therefore need to use american dates. But I can't get my DateTime object to "act" american. Let me give an example: In order to take in an american date, I tell it which culture the date will be formatted...
4
8778
by: dan688 | last post by:
Morning, Could'nt find a post / site that has addressed this issue and I would be very surprised if I'm the first to find this. I am currently having an issue passing a parameter via a stored procedure in vb6 to SQL 2005 that is requiring a datetime format. VB6 supports these separatly i.e. DATE, TIME were as VB.Net etc. support this as...
5
3616
by: Nick Gilbert | last post by:
Hi, One of our servers is behaving differently to all the others when printing dates. They seem to be outputting in American format (12 hour) when the default for a UK machine should be UK format (dd/mm/yyyy). The following code: DateTime.Now.ToString() DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.