|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Making my DateTime object think its American :-)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.TryParse(txtAvailableFrom.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 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.CurrentCulture, 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.CurrentCulture property. Ideally, what you should be doing is on ALL ToString and parse calls, pass CultureInfo.CurrentCulture where the information is displayed or taken from the user. Pass CultureInfo.InvariantCulture 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. Show quote > 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.TryParse(txtAvailableFrom.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 > [aspnet ng trimmed]
Simon Harvey wrote: > Hi all, The DateTime structure itself just stores a date and a time; it doesn't> > 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. know anything about formats. It's just a number, really. > whispers: DateTimePicker? :)> 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.TryParse(txtAvailableFrom.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. See above. As expected, in the absence of any explicit cultural> > 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) information, the local culture is used - which as you have told us is British. You wouldn't expect "2,3", entered in an app and parsed into a double (2.3) with fr-FR, to 'remember' that it's "2,3" not "2.3", would you? It's just a number. > The real showstopper for me though is when it comes to passing the date to Now this is confusing. If you have a SQL Server stored proc with a date> 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. type parameter, and you use a SqlParameter object to supply the value of this parameter, there shouldn't be a problem. The only way I can think of that you might get a problem is if the date is being converted to a string, supplied to a text parameter (eg a varchar) and converted back within the sproc. Is this happening? Let's see the code that invokes the sproc, along with the parameter-filling stuff. -- Larry Lard Replies to group please 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 Show quote "Simon Harvey" <notha***@hotmail.com> wrote in message news:7c72785b1eae28c8005f0484681e@news.microsoft.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.TryParse(txtAvailableFrom.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 > > Try this stuff in a web.config file...
<!--2/10/2006 10:52:49 AM --> <globalization culture="auto" uiCulture="auto" /> // on my "American" machine <!--2/10/2006 10:52:49 AM--> <globalization culture="en-US" uiCulture="en-US" /> <!--10/02/2006 10:55:56--> <globalization culture="en-GB" uiCulture="en-GB" /> <%= Clinton Gallagher NET csgallagher AT metromilwaukee.com URL http://clintongallagher.metromilwaukee.com/ Show quote "Simon Harvey" <notha***@hotmail.com> wrote in message news:7c72785b1eae28c8005f0484681e@news.microsoft.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.TryParse(txtAvailableFrom.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 > > 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 If you blokes would learn to spell properly and stop having John Cleese
attack us with Declarations of Revocation ( http://www.stephaniemiller.com/declarationofrevocation.htm ) you probably would not need to post questions like this. :-) Peter -- Show quoteCo-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.TryParse(txtAvailableFrom.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 > > > 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 Hehe. Obviously, they have got the flags backwards. We Yanks are all simply
transplanted British Subjects, as we so often forget. -- Show quoteCo-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 > > > Try this
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); or .... System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB"); This will set the date time control to work in American/British formating without screwing with your control panel settings 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. |
|||||||||||||||||||||||