|
-
September 24th, 2008, 09:51 AM
#1
DateTime.ToString()
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?
-
September 24th, 2008, 10:01 AM
#2
Re: DateTime.ToString()
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.
-
September 24th, 2008, 11:09 AM
#3
Re: DateTime.ToString()
this is exactly what i did.
but i was curious if the default is tied to the user or by machine.
-
September 24th, 2008, 11:10 AM
#4
Re: DateTime.ToString()
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:
Code:
DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture);
InvariantCulture is associated with the English language but not with any country/region.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
September 25th, 2008, 12:04 PM
#5
Re: DateTime.ToString()
You could also do the next thing
Code:
DateTime dt = DateTime.Now;
MessageBox.Show(dt.ToString("dd/MM/yyyy"));
gives a message that looks like 25-09-2008
Code:
DateTime dt = DateTime.Now;
MessageBox.Show(dt.ToString("MM/dd/yyyy"));
gives a message that looks like 09-25-2008
-
September 25th, 2008, 12:54 PM
#6
Re: DateTime.ToString()
try this:
Code:
DateTime datetime = DateTime.Now;
Console.WriteLine(datetime.ToString("MMM dd yyyy"));
output:
Sep 25 2008
Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]
-
September 25th, 2008, 02:04 PM
#7
Re: DateTime.ToString()
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.
-
September 26th, 2008, 09:26 AM
#8
Re: DateTime.ToString()
how do i dump the cultureInfo info? what is the C# .NET class/method to do this?
-
September 26th, 2008, 09:47 AM
#9
Re: DateTime.ToString()
Use Thread.CurrentThread.CurrentCulture or Thread.CurrentThread.CurrentUICulture to retrieve the current CultureInfo. See CultureInfo in Msdn on how to dump the values.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|