Visual Basic/Date and Time

Date and time functions in Visual Basic are treated here. See also VB6 Command Reference#Date and Time and Snippets#Useful Date Functions.

Some functions:

  • Now() - date and time of the present moment
  • Day() - day of the month, from 0 to 31
  • Year()
  • DateValue() - Given a timestamp, returns that timestamp with the time part cleared. Given a string, returns a timestamp with its date portion specified by that string.
    • DateNow = DateValue(Now)
  • TimeValue() - Given a timestamp, returns that timestamp with the date part cleared. Given a string, returns a timestamp with its time portion specified by that string.
    • TimeNow = TimeValue(Now)
  • Datepart()
  • Timepart()
  • Weekday() - 1-7
    • If Weekday(Now) = vbSaturday Then MsgBox "It is Saturday."
  • WeekdayName()
    • Localized name of the week day.
    • TodaysWeekdayName = WeekdayName(Weekday(Now, vbUseSystem))
      • Works even in non-U.S. locale.
  • Month()
  • MonthName()
  • Hour()
  • Minute()
  • Second()
  • DateAdd()
    • Later = DateAdd("d", 31, Now) ' 31 days later
  • DateDiff()
  • Format()
    • Formatted = Format(Now, "yyyy-MM-dd") ' e.g. 2016-09-17
  • FormatDateTime()
  • Timer()
  • IsDate()

Date and Time can be treated as a variables to be read from and written to. However, writing to them changes system date or time, which you usually do not want to do.

Some constants:

  • vbSunday = 1, vbMonday, vbTuesday, vbWednesday, vbThursday, vbFriday, vbSaturday = 7
  • vbGeneralDate = 0 , vbLongDate, vbShortDate, vbLongTime, vbShortTime = 4
edit