Many forms will have need to keep track of a date field. In such cases, it can be useful to write code to automatically get the date. This can be done easily using classes that are built into both VB.NET and Javascript.


Getting the date in VB.NET is a very simple process. It simply involves instantiating a Date object and initializing it as being today.


Dim todaysDate As Date = Date.Today


todaysDate now represents today's date. This can be manipulated in various ways.

If you wish to fill a field (such as a date field) with this date in MM/dd/yyyy format, you can do so as follows:


_DateField.Value = todaysDate.ToString("MM/dd/yyyy")


 The string inside the ToString() function call specifies the format of the requested date. If you do not use this format string, then you are not guaranteed to get the date in that format.


Another concept to note is that the Date class also holds information about time. Using Date.Today will default the time to 12:00:00 AM. If you want to include the current time, you can change the original declaration to:


Dim todaysDateAndTime As Date = Date.Now


For more information on dealing with the Date class, including all of the available properties and methods, check out Microsoft's documentation. Note that Date is VB.NET's representation of the DateTime class.


Javascript also has a built in way to get the date. The first step is the same as VB.Net, where you create a Date object:


var today = new Date()


This declaration will automatically initialize the object to today's date. 

Unlike in VB.NET, there is no easy to create a MM/dd/yyyy formatted string. Instead, you must format your own:


var day = today.getDate()
var month = today.getMonth() + 1
var year = today.getFullYear()

if (day < 10){
	day = "0" + day

if (month < 10){
	month = "0" + month

var dateString = month + "/" + day + "/" + year


An important item to note about Javascript is that the month is referenced as January being month 0. As such, if you want to create an accurate date string you must add one to the month. This can be seen on line 2 above. In addition, the month and date retrieved by calling their respective methods are not guaranteed to be in two-digit MM or DD format, so it may be necessary to add the leading 0 to both in order to obtain the proper string. For example, if the date were May 5th, getDate() would return 5 instead of 05, and getMonth() would return 5 instead of 05. In order to circumvent this issue, you must include a check that sees if these values are less than 10 (and so are only one digit), and if so, then you must add a leading 0. This can be seen on lines 5-11 above.

For more detailed documentation on using this class, you can check out the excellent W3 Schools page on Javascript's Date object.