Click to See Complete Forum and Search --> : DateTime.ToString()
timewaster
September 24th, 2008, 09:51 AM
I am using the C# .NET Datetime class DateTime and taking the default when converting to a string DateTime.ToString().
When i run it under a user i get the U.S. format - month then day, but when i run it on the SAME machine started up as a service, i get the European format - day then month?
why is that? when i run it under services, it is running under a different user. Could that be why? I thought the DateTime default locale was machine based rather than user based?
mmetzger
September 24th, 2008, 10:01 AM
Not sure about the details regarding the locale but your best bet may be to define the exact format you want in the ToString() method.
timewaster
September 24th, 2008, 11:09 AM
this is exactly what i did.
but i was curious if the default is tied to the user or by machine.
boudino
September 24th, 2008, 11:10 AM
It could be. Under service, there is no GUI, so no UI culture is set. If the format of the date matter, it is stronly recommended to specify a format string and a culture if calling ToString().
The simplies way how to to be sure in which format the date will be is this:
DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture);
InvariantCulture is associated with the English language but not with any country/region.
dannystommen
September 25th, 2008, 12:04 PM
You could also do the next thing
DateTime dt = DateTime.Now;
MessageBox.Show(dt.ToString("dd/MM/yyyy"));
gives a message that looks like 25-09-2008
DateTime dt = DateTime.Now;
MessageBox.Show(dt.ToString("MM/dd/yyyy"));
gives a message that looks like 09-25-2008
toraj58
September 25th, 2008, 12:54 PM
try this:
DateTime datetime = DateTime.Now;
Console.WriteLine(datetime.ToString("MMM dd yyyy"));
output:
Sep 25 2008
Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]
Arjay
September 25th, 2008, 02:04 PM
To unravel the mystery just dump the CultureInfo settings to the event log when running as a service or to the console or file if running under a user account.
timewaster
September 26th, 2008, 09:26 AM
how do i dump the cultureInfo info? what is the C# .NET class/method to do this?
Arjay
September 26th, 2008, 09:47 AM
Use Thread.CurrentThread.CurrentCulture or Thread.CurrentThread.CurrentUICulture to retrieve the current CultureInfo. See CultureInfo (http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(VS.80).aspx) in Msdn on how to dump the values.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.