Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

DateTime /File question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
csharpula csharp - 24 Mar 2008 13:36 GMT
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!
Jon Skeet [C# MVP] - 24 Mar 2008 13:58 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gilles Kohl [MVP] - 24 Mar 2008 14:13 GMT
>Hello,
>I am trying to add Date and Current time to a name of a log file. I
[quoted text clipped - 8 lines]
>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.
Gilles Kohl [MVP] - 24 Mar 2008 14:22 GMT
>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.
BlackWasp - 24 Mar 2008 22:32 GMT
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

Signature

BlackWasp
www.blackwasp.co.uk

> Hello,
> I am trying to add Date and Current time to a name of a log file. I
[quoted text clipped - 12 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Jon Skeet [C# MVP] - 24 Mar 2008 22:50 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

BlackWasp - 25 Mar 2008 20:44 GMT
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.

Signature

BlackWasp
www.blackwasp.co.uk

>> You may want to avoid using yyyymmdd and the like if your application
>> needs
[quoted text clipped - 6 lines]
> the support team to be able to know exactly what a filename means when
> they get the logs shipped to them.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.