Tuesday, September 21, 2010

Visual Basic Date and Time Function : DateAdd

Purpose : The DateAdd function returns a variant (date data ) containing a date to which a specified time interval has been added.

Syntax :
DateAdd ( interval, number, date )

interval : The interval of time you want to add that is string expression ( see in blog )
number : the number of intervals you want to add. also use positive and negative value
date : Date to which the interval is added

The interval argument has these setting
Setting Description
yyyy year
q Quarter
m Month
y Day of year
d day
w weekday
h hour
n Minute
s Secound



DateAdd ("m", 1, " 31-Jan-2010")

Return value is 28-Feb-2010

Visual Basic Date and Time Function : Now

Purpose : The Now function provides the current date and time of the computer system's clock/calendar as date data type.

Syntax :
Now ( )

Option Explicit

Private Sub Timer1_Timer()
'set timer interval
Timer1.Interval = 100
'display the date variant
Text1.Text = Str$ (Now)
'display formatted date and time
Text2.Text = Format$ (Now(), "mmmm-dd-yyyy hh:mm:ss")
End Sub

The Now function has no arguments. the date returned by the Now function represents the system date and time at the moment the code runs.

Visual Basic Date and Time Function : DateValue

Purpose : The dateValue function converts a date in the form of a string into a visual basic date data type . This function changes differently formatted dates to a universal form.

Syntax :
DateValue ( datestring$ )

datestring$ : String that represents a date for the dateValue function to convert.

Examples of valid datestring$ Argument values
Valid String
01/01/2010
01/01/10
January 1, 2010
Jan 1, 1994
01-Jan-2010
01 January 2010

Option Explicit

Private Sub cmdCalculate_Click()
Dim dtmFirstDate As Date, dtmEndDate As Date
Dim vmtDays As Variant
On Error GoTo BadDate
If txtDate(0).Text = "" Or txtDate(1).Text = "" Then
ErrMsgBox "Please enter a date in each box."
Exit Sub
End If
'find date variant of entered date
dtmFirstDate = DateValue(txtDate(0).Text)
'find date variant of entered date
dtmEndDate = DateValue(txtDate(1).Text)
'display date variant
txtDate(0).Text = Str(dtmFirstDate)
'display date variant
txtDate(1).Text = Str(dtmEndDate)
'find number of days between dates
vmtDays = Abs((dtmEndDate - dtmFirstDate))
lblDifference.Caption = "Number of days between dates : " & _
Trim(vmtDays) & " Days"
Exit Sub

BadDate:
ErrMsgBox "Please enter a valid date."
Exit Sub

End Sub

Public Sub ErrMsgBox (massage As String)
MsgBox massage
End Sub

Monday, September 20, 2010

Visual Basic Date and Time Function : DateSerial

Purpose : The DateSerial function converts the numeric values of an indicated date to a Visual Basic date data type.

Syntax :
DateSerial ( year%, month%, day% )

year% : A number or expression that evaluates to between 100 and 9999 inclusive
month% : A number or expression that evaluates to between 1 and 12 inclusive
day% : A number or expression that evaluates to between 1 and 31 inclusive

Option Explicit

Dim thisYear%, thisMonth%, thisDay%, monthsToAdd%
Dim newDate As Date


Private Sub Command1_Click()
'how many months to add to current date
monthsToAdd% = Val(Combo1.Text)
thisYear% = Year(Now())
thisMonth% = Month(Now())
thisDay% = Day(Now())
On Error GoTo baddate
newDate = DateSerial(thisYear%, thisMonth% + monthsToAdd%, thisDay)
Text1.Text = Format$(newDate, "dd-mm-yyyy")
Exit Sub

baddate:
MsgBox "The result date is not acceptable"
Exit Sub

End Sub

Private Sub Form_Load()
Dim i%
For i = 1 To 12
Combo1.AddItem i
Next i
Combo1.ListIndex = 0
End Sub

Visual Basic Date and Time Statements : Date, Date$

Purpose : The Date and Date$ statement enable the user to change the system date of a computer within a visual Basic application.

Syntax :
Date = expression$
Date = datestring$

datestring$: String that represents the date in the form of mm-dd-yy or mm-dd-yyyy
expression$: string that represents the date ( more generalized than datesrting$ )

Acceptable contents of the datestring$ argument
datestring$ Setting
mm a number between 01 and 12 inclusive
dd a number between 01 and 31 inclusive
yy a number between 00 and 99 inclusive
yyyy a number between 1980 and 2079 ( on windows NT; 2099 on windows 95 inclusive