VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

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

In most cases, you don't need any special functions to perform date arithmetic. All you need to know is that the integer part in a Date variable holds the date information, and the fractional part holds the time information:

' 2 days and 12 hours from now
Print Now + 2 + #12:00# ' Displays "8/17/2008 8:35:48 A.M."

For more sophisticated date math, you can use the DateAdd function, for which the syntax is the following:

NewDate = DateAdd(interval, number, date)

The interval is a string that indicates a date or time unit (see table below), number is the number of units you are adding, and date is the starting date. You can use this function to add and subtract date and time values:

' The date three months from now
Print DateAdd("m", 3, Now) ' Displays "11/14/2008 8:35:48 P.M."
' One year ago (automatically accounts for leap years)
Print DateAdd("yyyy", -1, Now) ' Displays "8/14/2007 8:35:48 P.M."
' The number of months since Jan 30, 2008
Print DateDiff("m", #1/30/2008#, Now) ' Displays "7"
' The number of days since Jan 30, 2008 _ you can use "d" or "y".
Print DateDiff("y", #1/30/2008#, Now) ' Displays "196"
' The number of entire weeks since Jan 30, 2008
Print DateDiff("w", #1/30/2008#, Now) ' Displays "28"
' The number of weekends before 21st century - value <0 means
' future dates.
' Note: use "ww" to return the number of Sundays in the date interval.
Print DateDiff("ww", #1/1/2000#, Now) ' Displays "-72"

When you have two dates and you want to evaluate the difference between them—that is, the time elapsed between one date and the next—you should use the DateDiff function, for which the syntax is

Result = DateDiff(interval, startdate, enddate _
[, FirstDayOfWeek[, FirstWeekOfYear]])

where interval has the meaning shown in the previous page Building and Extracting Date and Time Values's table, FirstDayOfWeek is an optional argument that you can use to specify which weekday should be considered as the first day of the week (you can use the constants vbSunday, vbMonday, and so on), and FirstWeekOfYear is another optional argument that lets you specify which week should be considered as the first week of the year.

Possible values for the FirstWeekOfYear argument in the DateDiff function.

Constant Value Description
vbUseSystem 0 Use the NLS API setting.
vbFirstJan1 1 The first week is the one that includes January 1. (This is the default value for this setting.)
vbFirstFourDays 2 The first week is the first one that has at least four days in the new year.
vbFirstFullWeek 3 This first week is the first one that's completely contained in the new year.

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