Get DateTimeFormatInfo for user?

  • Thread starter Thread starter Claes Bergefall
  • Start date Start date
C

Claes Bergefall

I would like to format dates etc in the format that the user browsing the
page would prefer according to his language setting. The DateTimeFormatInfo
class provides all the information I need, but how do I get an instance of
it (or something else that has the info)?

Using DateTimeFormatInfo.Current doesn't work. It gives me the server
settings.
Using HttpRequest.UserLanguages to create a culture doesn't work either. The
language I get from that collection doesn't contain any country/region code
so I get a neutral culture that doesn't have any DataTimeFormatInfo

Is there a way to get the prefered date and time settings for the user that
requested the page?

/claes
 
Hi,

Thank you for posting!

ASP.NET 2.0 has a new feature called 'Auto-Culture Handling': it maps
Accept-Language headers to CultureInfo objects and attaches them to the
current thread. Auto-Culture handling can be enabled for individual pages
by including 'Culture="auto" UICulture="auto"' attributes in the @ Page
directive. It can be enabled site-wide by including a <globalization
culture="auto" uiCulture="auto"/> element in web.config.

Since DataTime's format functions such as ToShortDateString() automatically
uses formatting information derived from the current culture, so for your
requirement, I think simply call these functions without specifying a
DateTimeFormatInfo would be suffice.

If there's anything unclear, please feel free to post here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ok, sounds cool. I'll try that
Any way to get hold of this "auto-culture" from code? I would like to get
some info from the corresponding DateTimeFormatInfo, such as the
FirstDayOfWeek property and DayNames collection

/claes
 
Never mind. I see that I actually get the localized DateTimeFormatInfo using
DateTimeFormatInfo.Current
Thank you. Works perfect now

/claes
 
You are welcome.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top