411 REFERENCE · Wikibooks
Visual Basic/Date and Time
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
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
External links
Date Time Functions In Visual Basic, vb6.us
DateAdd Function - Visual Basic 6.0, chennaiiq.com
Formating dates and times in VB6, vb6.us
WeekdayName Function - Visual Basic 6.0, chennaiiq.com
Finding the Beginning or End of a Week, msdn.microsoft.com
DateDiff Function - Visual Basic 6.0, chennaiiq.com