C# how to keep datetimes independent of regional settings
-
i am taking the system date as
DateTime.Now.Date
its giving diffetent values for diferent culture 6/5/2010 12:00:00 AM : for English(United State) 05/06/2010 00:00:00 : for English(United Kingdom) I want my dates in '6/5/2010 12:00:00 AM' how to get date independent of regional settings -
i am taking the system date as
DateTime.Now.Date
its giving diffetent values for diferent culture 6/5/2010 12:00:00 AM : for English(United State) 05/06/2010 00:00:00 : for English(United Kingdom) I want my dates in '6/5/2010 12:00:00 AM' how to get date independent of regional settingsDateTime is just a long counting ticks IIRC - what you are seeing is just the string representation for the culture in question. Specify your own string format, culture or InvariantCulture. PiebaldConsult will be here shortly with his ISO comments!
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
i am taking the system date as
DateTime.Now.Date
its giving diffetent values for diferent culture 6/5/2010 12:00:00 AM : for English(United State) 05/06/2010 00:00:00 : for English(United Kingdom) I want my dates in '6/5/2010 12:00:00 AM' how to get date independent of regional settingsAs Davey says, DateTime.Now just returns a time - it has no format. When you specify the output format, then you can use the current culture, specify a culture, or fix the format so that it is culture invariant. Have a look here[^] - it shows all the various parts you can use to format your date.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
i am taking the system date as
DateTime.Now.Date
its giving diffetent values for diferent culture 6/5/2010 12:00:00 AM : for English(United State) 05/06/2010 00:00:00 : for English(United Kingdom) I want my dates in '6/5/2010 12:00:00 AM' how to get date independent of regional settingsAs others have pointed out, a DateTime is a region independent structure already. What you are talking about is using a format converter to control how it is displayed. Before you look at setting the output of the date to be a particular format, I must ask you to consider how your application is going to be used; people have a specific local regional setting in place and generally expect their dates to be displayed in those formats - this can be critical because it has an effect on the understanding of the date. When I looked at your example, my first thought was that you were talking about the 6th of May because I am based in the UK, by displaying a different regional setting you have skewed my understanding of your data.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
i am taking the system date as
DateTime.Now.Date
its giving diffetent values for diferent culture 6/5/2010 12:00:00 AM : for English(United State) 05/06/2010 00:00:00 : for English(United Kingdom) I want my dates in '6/5/2010 12:00:00 AM' how to get date independent of regional settingsAh, DateTime, a perfect source of confusion around the globe. You might want to read this[^]. It holds some cues, including a reference to the infamous ISO 8601. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Ah, DateTime, a perfect source of confusion around the globe. You might want to read this[^]. It holds some cues, including a reference to the infamous ISO 8601. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).