DateTime /File question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    DateTime /File question


    Hello,
    I am trying to add Date and Current time to a name of a log file. I
    tried doing this:

    string logDateExtentio n = DateTime.Now.To ShortDateString ().ToString() +
    DateTime.Now.To ShortTimeString ().ToString();

    string logName = Data.Name + Data.ProductNam e + logDateExtentio n;

    And this throws exception during the debug that the name is not legal
    probably because of the ":" or "/" . Which format of date will work for
    this purpose?

    Thanks!


    *** Sent via Developersdex http://www.developersdex.com ***
  • Gilles Kohl [MVP]

    #2
    Re: DateTime /File question

    On Mon, 24 Mar 2008 05:36:18 -0700, csharpula csharp <csharpula@yaho o.com>
    wrote:
    >
    >Hello,
    >I am trying to add Date and Current time to a name of a log file. I
    >tried doing this:
    >
    >string logDateExtentio n = DateTime.Now.To ShortDateString ().ToString() +
    >DateTime.Now.T oShortTimeStrin g().ToString();
    >
    string logName = Data.Name + Data.ProductNam e + logDateExtentio n;
    >
    >And this throws exception during the debug that the name is not legal
    >probably because of the ":" or "/" . Which format of date will work for
    >this purpose?
    My suggestion would be someting like YYYYMMDD (maybe separate them using
    underlines) because that also sorts well.

    Use e.g.

    logDateExtensio n = DateTime.Now.To String("yyyyMMd d");

    or

    logDateExtensio n = DateTime.Now.To String("yyyy_MM _dd");

    Regards,
    Gilles.

    Comment

    • BlackWasp

      #3
      Re: DateTime /File question

      You may want to avoid using yyyymmdd and the like if your application needs
      to use the user's preference / locale for formatting. In which case, use
      "g" as the format string to combine short date and short time.

      Check out my article on DateTime manipulation, which explains the various
      formatting options for more info.



      --

      BlackWasp



      "csharpula csharp" <csharpula@yaho o.comwrote in message
      news:ubqTpuajIH A.3940@TK2MSFTN GP05.phx.gbl...
      >
      Hello,
      I am trying to add Date and Current time to a name of a log file. I
      tried doing this:
      >
      string logDateExtentio n = DateTime.Now.To ShortDateString ().ToString() +
      DateTime.Now.To ShortTimeString ().ToString();
      >
      string logName = Data.Name + Data.ProductNam e + logDateExtentio n;
      >
      And this throws exception during the debug that the name is not legal
      probably because of the ":" or "/" . Which format of date will work for
      this purpose?
      >
      Thanks!
      >
      >
      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      Working...