Reocurring programming tasks - Part I: Handling DateTime
Even as an architect I sometimes need to get some hands-on experience regarding new technologies and and new versions or frameworks which involve programmin tasks. Prominent examples for this are the WinFX technologies. Unfortunately as a casual and deliberately average programmer I often forget what the trick is with some reocurring patterns or tasks. So from time to time I will post the task and the common solution here for me as a reminder in the first place but also for all of you who feel the same pain. And I'm sure at least some of you will find those helpful although those little helpers don't deal with the super duper state of the art architectural or business process related topics ;-).
One of those reocurring things is acquiring the current date and time. Unfortunately the DateTime struct and its application is not as self explaining as one might wish especially when it comes to formatting the result of a respective call.
The basic way in the .Net framework 2.0 is to use the really comfortable DateTime struct in the System namespace. With DateTime it is usually quite easy to get thh system date and time of the system that executes the code.
The current date and time information is stored in the Now property of DateTime. However the information is not formatted so that you will only get some not very meaningful numbers when calling the toString() method. To solve this there are overloads of the toString() method to pass in a parameter which indicates the required formatting. So if you pass in a "d" for example is the short format of the date information wheras the "t" returns the short format of the time information. A complete list of formatting keys can be found here:
public void HelloDate()
Returns: Friday, March 3, 2006