ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
Connections, Commands, And Procedures
What is ASP?
VBScript Classes: Part 1 of N

Previewing ASP pages in Visual InterDev 6.0
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Quick Tips

Sortable Date Format

I was recently working on a project which needed to send daily reports to a vendor via a web service. As I was building the application, it became apparent that the source of the data was date-dependent and that recreating a previous day's report was non-trivial. In my mind, this meant that we absolutely needed some sort of log file. That way, as long as we were able to get the data to begin with, even if an error occurred in the transmission or something happened on the vendor's end, we'd know what information was involved and would be able to easily recreate the transaction.

As an aside, I've always been a fan of using log files for this type of stand-alone application. Sure you could log the information to a database, but that adds additional complexity and more chance for error. Log files are simple to implement, easy to read, and relatively hard to break. After all, if your server's drives aren't working, then most likely your application is down which means there's nothing to log anyway!

This brings me to the point of this tip. I've seen lots of other developers do this same type of thing, but the thing that always stumps me is the file naming schemes some of them come up with. Sure you might want to add a prefix or suffix to the file, but if you're creating a daily log file, it only makes sense to name it after the date it was created. Furthermore, why do people insist on using month, day, year? For example, using "04152008" for April 15, 2008. Sure it works great for the first few months, but next year when you go to look for a log file, the one for April 15, 2009 will be right next to it. Does it really make sense to have two files, that were created a year apart, right next to each other? Why not use a naming scheme that when sorted numerically puts the files in the appropriate chronological order?

The format I've been using for some time is quite simple: April 15, 2008 is "20080415". It goes from largest unit to smallest unit so that all transactions for the same year, quarter, month, or week are all grouped together. You can add separator characters if you like ("2008-04-15"), but I think you'll find it's probably not even necessary. Once you get used to the format and thinking from largest date unit to smallest, it just seems to make sense.

So the question then becomes, how do you construct the file name? Well it's simpler then you'd think. In .NET, to get today's date in the format described, simply use the Format function:

strLogFileName = Format(Now(), "yyyyMMdd")

For more information on VB's Format function, check out the documentation on MSDN: Format Function. For more on custom formats, see: User-Defined Date/Time Formats

Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to:

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers