DateTime /File question

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

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

string logDateExtention = DateTime.Now.ToShortDateString().ToString() +
DateTime.Now.ToShortTimeString().ToString();

string logName = Data.Name + Data.ProductName + logDateExtention;

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!
 
csharpula csharp said:
I am trying to add Date and Current time to a name of a log file. I
tried doing this:

string logDateExtention = DateTime.Now.ToShortDateString().ToString() +
DateTime.Now.ToShortTimeString().ToString();

Are you aware that your extra calls to ToString() are unnecessary
there?
string logName = Data.Name + Data.ProductName + logDateExtention;

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?

I would go for this:

string logDateExtension = DateTime.Now.ToString("yyyyddMM-HHmmss");

(I'd probably also add ".log" on the end.)

One benefit of that extension is that it sorts nicely - if you sort by
filename, you'll get it sorted by time as well.

You may wish to consider using UtcNow instead of Now, so that things
don't confusing if the timezone is changed, or around daylight savings
time changes.
 
Hello,
I am trying to add Date and Current time to a name of a log file. I
tried doing this:

string logDateExtention = DateTime.Now.ToShortDateString().ToString() +
DateTime.Now.ToShortTimeString().ToString();

string logName = Data.Name + Data.ProductName + logDateExtention;

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.

logDateExtension = DateTime.Now.ToString("yyyyMMdd");

or

logDateExtension = DateTime.Now.ToString("yyyy_MM_dd");

Regards,
Gilles.
 
My suggestion would be someting like YYYYMMDD (maybe separate them using
underlines) because that also sorts well.
[...]

Use e.g.

logDateExtension = DateTime.Now.ToString("yyyyMMdd");

Oops, didn't see that you also wanted time, but Jon already provided that.

You may want to consider using month-day order for sorting reasons.

Regards,
Gilles.
 
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.

http://www.blackwasp.co.uk/CSharpDateManipulation.aspx
 
BlackWasp said:
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.

I would say that formatting the *log filename* according to the user's
locale is a recipe for problems. I think this is one case where
localisation shouldn't be applied. In particular, it's very useful for
the support team to be able to know exactly what a filename means when
they get the logs shipped to them.
 
Hi Jon,

I agree with you on the concept. Just here the original question included
the use of culture-sensitive information.

I personally would probably use UTC for multi-location systems and have done
with it.

--

BlackWasp
www.blackwasp.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top