VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
Error Handling (2)
VB6 Database

You are here: Visual Basic > VB6 (Beginners Tutorial)

Previous Page | Table of Contents | Next Page

Date Arithmetic - VB6 Date & Time

The most important and flexible function for formatting date and time values is the Format function. This function gives you seven different, named formats for date and time:

  • General Date (date and time in general format; only the date if the fractional part is 0; only the time if the integer part is 0)

  • Long Date (for example, Friday, October 17, 2008, but results vary depending on your locale)

  • Medium Date (for example, 17-Oct-2008)

  • Short Date (for example, 10/17/2008)

  • Long Time (for example, 8:35:48)

  • Medium Time (for example, 8:35 A.M.)

  • Short Time (for example, 8:35 in a 24 hour format)

You also have a few special characters with which you can build your own custom date and time format strings, including one- and two-digit day and month numbers, complete or abbreviated month and weekday names, a.m/p.m. indicators, week and quarter numbers, and so on:

' mmm/ddd = abbreviated month/weekday,
' mmmm/dddd = complete month/weekday
Print Format(Now, "mmm dd, yyyy (dddd)") ' "Aug 14, 1998 (Friday)"
' hh/mm/ss always use two digits, h/m/s use one or two digits
Print Format(Now, "hh:mm:ss") ' "20:35:48"
Print Format(Now, "h:mm AMPM") ' "8:35 P.M."
' y=day in the year, ww=week in the year, q=quarter in the year
' Note how a backslash can be used to specify literal characters.
Print Format(Now, "mm/dd/yy (\d\a\y=y \w\e\e\k=ww \q\u\a\r\t\e\r=q)")
' Displays "08/14/98 (day=226 week=33 quarter=3)"

Visual Basic 6 has introduced the new FormatDateTime function. It's far less flexible than the standard Format function and permits only a subset of the Format function's named formats. The only advantage of the FormatDateTime function is that it's also supported under VBScript and so can contribute to the ease of porting pieces of code from Visual Basic and VBA to VBScript and vice versa. Its syntax is

result = FormatDateTime(Expression, [NamedFormat])

where NamedFormat can be one of the following intrinsic constants: 0-vbGeneralDate (the default), 1-vbLongDate, 2-vbShortDate, 3-vbLongTime, or 4-vbShortTime. Here are a few examples:

Print FormatDateTime(Now) ' "8/14/98 8:35:48 P.M."
Print FormatDateTime(Now, vbLongDate) ' "Saturday, August 15, 1998"
Print FormatDateTime(Now, vbShortTime) ' "20:35"

Visual Basic 6 also includes two new functions related to date formatting. The MonthName function returns the complete or abbreviated name of a month, whereas the WeekdayName function returns the complete or abbreviated name of a weekday. Both are locale aware, so you can use them to list month and weekday names in the language the operating system has been configured for:

Print MonthName(2) ' "February"
Print MonthName(2, True) ' "Feb"
Print WeekdayName(1, True) ' "Sun"

More on Date and Time in Visual Basic 6


Previous Page | Table of Contents | Next Page


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved