Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing DateTimeFormat in a Windows Service 1

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I had a Windows application that was processing files and as part of that was converting strings into dates.

In the Windows app - the entry of 'Nov-08' was being converted to a date with:
- day = 1
- month = 11
- year = 2008

However within a Windows service (using the same code as the Windows application) I get the same entry of 'Nov-08' being converted into a date with:
- day = 8
- month 11
- year = 2009

On further investigation I'm finding the 'System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern' value differs between the Windows application and the Windows service.

The Windows application has this set as 'dd/MM/yyyy' - whereas the Windows service has this set as 'M/d/yyyy'.
Which then explains the string conversions to sates that I am seeing.

How can I read an entry of 'Nov-08' through the Windows service in the same way as through the Windows application (version).

The entry format 'Nov-08' is not fixed - but the string should be read using the 'dd/MM/yyyy' date format.

How can this be done?
I've tried changing the value of System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern - but this is found to be read only.

Any help / pointers would be appreciated.
Thanks in advance.

Steve
 
try setting the System.Globalization.CultureInfo to a new culture info. it will be something like this where you are replacing the overall globalization.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks. I was able to make use of somthing like this successfully.

I used:

Code:
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB");

DateTime d = Convert.ToDateTime(myDateString, culture);
 
If you're having problems 'cos your server is running with a different CultureInfo (eg our live server is set up for en-US, even though we're in the Uk) you can change the culture the app runs in using the app or web.config Globalization Element.
Code:
    <globalization 
            culture="en-GB" 
            uiCulture="en-GB" 
            requestEncoding="utf-8" 
            responseEncoding="utf-8" 
   />

hth

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
This is a good example of how services run as a different user than the person with their hands on the keyboard.

(Since date formats are stored on a per-user basis)

Chip H.



____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top