CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2007
    Posts
    19

    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?

  2. #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.

  3. #3
    Join Date
    Jan 2007
    Posts
    19

    Re: DateTime.ToString()

    this is exactly what i did.
    but i was curious if the default is tied to the user or by machine.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    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

  6. #6
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Lightbulb 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]

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  8. #8
    Join Date
    Jan 2007
    Posts
    19

    Re: DateTime.ToString()

    how do i dump the cultureInfo info? what is the C# .NET class/method to do this?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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
  •  





Click Here to Expand Forum to Full Width

Featured