For the sake of testing, the Web Server is on a computer with Maori
culture where the short date format like this: d/MM/yyyy
.
The user is on a computer with Hungarian
culture and the short date format is yyyy.MM.dd.
I've created a webpage that will display DateTime.Now on a label when the page is loaded. The displayed date is 2/18/2013 2:22:06 PM
.
So now my question is, how come the displayed date is in another format/culture, which I suspect Eng-US?
Secondly, I will need to compare date from database (assuming the database is on a computer with a different culture) to DateTime.Now. To do this, I plan to convert the date from database to match the culture of the Web Server. But given the situation in my first question, how do I know in which culture/format DateTime.Now will be in?
EDIT: The code used:
DateTime nowDateTime = DateTime.Now;
Label7.Text = nowDateTime.ToString().Trim();
For the first part of your question: your code is basically just using the current thread culture. You could either make sure that the current thread culture for each request is set to a suitable culture for that request (based on accept headers etc - see HttpRequest.UserLanguages
) or you could pass that explicitly to all calls to ToString
etc.
As for why it's coming up in US English format - that may well be the culture that the web-serving service is running as. That may well not be the same as the culture that the desktop uses. You should take pains to make sure that's not used except possibly as a fallback if the user request doesn't provide any information. (I'd be tempted to pick an explicit fallback rather than using the default for the service.)
Secondly, I will need to compare date from database (assuming the database is on a computer with a different culture) to DateTime.Now.
Using DateTime.Now
is almost certainly a bad idea. In most cases, you should use DateTime.UtcNow
and store UTC date/times in the database. (There are cases where you should instead store a local time and a time zone, but even then you wouldn't use DateTime.Now
.)
To do this, I plan to convert the date from database to match the culture of the Web Server. But given the situation in my first question, how do I know in which culture/format DateTime.Now will be in?
The culture is primarily relevant when converting to and from string representations. You should avoid any such conversion when either storing a value in the database or retrieving it. Instead, make sure that you're using an appropriate field type in the database ( DATETIME
or whatever) and then fetch it as a DateTime
in your data access layer. If you find yourself trying to use DateTime.Parse
or anything similar, that should ring alarm bells. You haven't told us what data access technology you're using, but pretty much everything allows you to retrieve a DateTime
directly. (eg DbDataReader.GetDateTime
.)
when comparing dates in different time zones it would make sense to store it in the database as UTC. Then you could easily compare it with any other date from any time zone (again converted to UTC).
Hope this helps.
Based on your edit, I don't think the culture is really Maori on the relevant thread. Try this to test:
Label7.Text = CultureInfo.CurrentCulture.ToString();
If it is "mi"
or "mi-NZ"
, it is Maori. Something with "en"
would be English, "hu"
Hungarian, and so on.
Note that if the current culture is changed programmatically, this affects only the current thread. Not other threads. But did you try to set the locale in the Control Panel of Windows?
How come the displayed date is in another format/culture, which I suspect Eng-US?
Answer: DateTime.Now
which renders 2/18/2013 2:22:06 PM
on a label is
already
in
English culture which may be due to current web server configurations or it is picking user's request culture. Maori
Possible solution: To render it in client's culture ie Hungarian Culture
Page
's InitializeCulture
event and change Culture
for the executing HttpRequest
based on user's request Header Accept-Language
which should be something like Accept-Language: hu-HU,en;q=0.8
Code Sample for 1 point above
<system.web>
<globalization
enableclientbasedculture="true"
uiculture="auto"
culture="auto">
</globalization>
</system.web>
For this Refer Scott Hanselman's Post on Globalisation of which concept can be applied to Web Forms Applications also.
For reading database values use code snippet as,
var culture = CultureInfo.CreateSpecificCulture("mi-NZ");
DateTime.Parse(sqlDbReader["MyDateColumnName"], culture)
Refer: Format SqlDataReader as date and MSDN - DateTime.Parse Method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.