Since I write on a daily base unit tests, I’m wondering more and more why there is not an easy – and especially unambiguous – way to provide a hardcoded date to a variable or procedure argument. Do you know, without any doubt, what this date actually means?
Dim thisYearsBirthday As Date = #5/6/2015#
Was I born on the 5th of June or the 6th of May? Visual Basic .NET will support now a good ISO-standard, which starts with the year. It follows the convention of ‘yyyy-MM-dd’ and you can even use slashes as separators. You can also provide the time if you want.
Dim todayDateWithDash = #2015-02-13# Dim todayDateWithSlash = #2015/2/13# Dim nowDateTime24 = #2015-02-13 19:59:00# Dim nowDateTime12 = #2015-02-13 7:59:00 PM# Console.WriteLine(todayDateWithDash.ToLongDateString()) Console.WriteLine(todayDateWithSlash.ToLongDateString()) Console.WriteLine(nowDateTime24.ToString("yyyy-MM-dd HH:mm:ss")) Console.WriteLine(nowDateTime12.ToString("yyyy-MM-dd hh:mm:ss"))
The results are shown in the screenshot below. Please note that you can specify the time part as 24-hour format as well as the 12-hour format. The
ToLongDateString() extension methods formats the dates to the Dutch language, but I’m sure that you will understand the concept.
BTW… for the people who are still doubting about my birthdate… I was born on a 6th of May!