DateTime and TimeSpan also have many methods and functions corresponding to them. For example, to get Today's date, we can use the following:

Dim TodayDate As DateTime = DateTime.Now

Now, if we wanted to add 100 days to today's date, we can simply do the following:

TodayDate.AddDays(100)

Also, if we wanted the year of the new date, we can use something like this:

TodayDate.Year

Now that we understand these basics, we can move on to the more complex methods. For example, if we want to subtract one DateTime object from another, we can easily use the .Subtract() function. Here is an easy example to understand:

Dim CurrentDate As DateTime = DateTime.Now
Dim FutureDate As DateTime = CurrentDate.AddHours(30)
Dim Difference as TimeSpan = FutureDate.Subtract(CurrentDate)

Now we have the difference between these two DateTime variables in a TimeSpan variable named "Difference." If we wanted to convert this difference to only minutes, we can do the following:

Dim intMinutes As Integer = Difference.TotalMinutes

NOTE: The .Add() and .Subtract() functions can be easily replaced by their Mathematic equivalents (+ or -, respectively).

DateTime has an abundance of useful functions, including the following:

  • .Now - Returns the current date and time as a DateTime object.
  • .Today - Returns the current date as a DateTime object, but leaves the time at 00:00:00.
  • .Year - Returns the year of the DateTime as an integer.
  • .Month - Returns the month of the DateTime as an integer.
  • .Day - Returns the day of the month of the DateTime as an integer.
  • .Hour - Returns the Hour of the DateTime as an integer. This is NOT military time. For example, if the time is 2:00 PM, this function will return 2, not 14.
  • .Minute - Returns the minute of the hour of the DateTime as an integer.
  • .Second - Returns the second of the minute of the DateTime as an integer.
  • .Millisecond - Returns the millisecond of the second of the DateTime as an integer.
  • .Date - Forms the datetime to a short hand, easy to read DateTime.
  • .DayOfWeek - Returns day of week. This can be returned as a string ("Sunday" through "Saturday"), or as an integer (0-6).
  • .Add(TimeSpan) - Adds a TimeSpan object to a DateTime object, and returns the new DateTime.
  • .Subtract(TimeSpan) - Subtracts a TimeSpan object from a DateTime object, and returns the new DateTime.
  • .AddYears(int) - Adds the specified number of years (integer) to the DateTime. This function can accept negative numbers to subtract years.
  • .AddMonths(int) - Adds the specified number of months (integer) to the DateTime. This function can accept negative numbers to subtract months.
  • .AddDays(int) - Adds the specified number of days (integer) to the DateTime. This function can accept negative numbers to subtract days.
  • .AddHours(int) - Adds the specified number of hours (integer) to the DateTime. This function can accept negative numbers to subtract hours.
  • .AddMinutes(int) - Adds the specified number of minutes (integer) to the DateTime. This function can accept negative numbers to subtract minutes.
  • .AddSeconds(int) - Adds the specified number of seconds (integer) to the DateTime. This function can accept negative numbers to subtract seconds.
  • .AddMilliseconds(int) - Adds the specified number of milliseconds (integer) to the DateTime. This function can accept negative numbers to subtract milliseconds.
  • .DaysInMonth() - Returns the number of days of the month of the DateTime.
  • .IsLeapYear() - Returns a Boolean object (True/False) depending on if the year of the DateTime is a leap year.
  • .ToString() - Returns a String of the DateTime object.
  • .ToString(String) - Returns a String of the DateTime object. This function allows you to specify the structure of the returned string.

TimeSpan has many helpful functions as well, including:

  • .Days - Returns the number of whole days in the TimeSpan as an integer.
  • .Hours - Returns the number of whole hours in the TimeSpan as an integer.
  • .Minutes - Returns the number of whole minutes in the TimeSpan as an integer.
  • .Seconds - Returns the number of whole seconds in the TimeSpan as an integer.
  • .Milliseconds - Returns the number of whole seconds in the TimeSpan as an integer.
  • .TotalDays - Returns the number of days in the TimeSpan as a double. This may include a fractional value.
  • .TotalHours - Returns the number of hours in the TimeSpan as a double. This may include a fractional value.
  • .TotalMinutes - Returns the number of minutes in the TimeSpan as a double. This may include a fractional value.
  • .TotalSeconds - Returns the number of seconds in the TimeSpan as a double. This may include a fractional value.
  • .TotalMilliseconds - Returns the number of milliseconds in the TimeSpan as a double. This may include a fractional value.
  • .Add(TimeSpan) - Returns a TimeSpan object of the two TimeSpan objects combined.
  • .Subtract(TimeSpan) - Returns a TimeSpan object of the original TimeSpan after the argument has been removed.
  • .FromDays(int) - Returns a TimeSpan object as many days as the argument provides. For example:
    Dim NewTime as TimeSpan = TimeSpan.FromDays(3)

    This will return a TimeSpan object that is 3 days long.

  • .FromHours(int) - Returns a TimeSpan object as many hours as the argument provides.
  • .FromMinutes(int) - Returns a TimeSpan object as many minutes as the argument provides.
  • .FromSeconds(int) - Returns a TimeSpan object as many seconds as the argument provides.
  • .FromMilliseconds(int) - Returns a TimeSpan object as many milliseconds as the argument provides.
  • .ToString() - Returns a String of the TimeSpan object.
  • .ToString(String) - Returns a String of the TimeSpan object. This function allows you to specify the structure of the returned string.

Next we will discuss arrays and the functions available for them, as well.

Previous Next

Startup Discount
Small Business?
Contact us for an Enormous Discount!