ProgrammerGuide.Net | Step by Step Programmer Guide

VB-Date/Time

Date/Time,

Date
Returns a Variant (Date) containing the current system date.
Syntax: Date
Remarks
To set the system date, use the Date statement.
Date, and if the calendar is Gregorian, Date$ behavior is unchanged by the Calendar property setting. If the calendar is Hijri, Date$ returns a 10-character string of the form mm-dd-yyyy, where mm (01-12), dd (01-30) and yyyy (1400-1523) are the Hijri month, day and year. The equivalent Gregorian range is Jan 1, 1980 through Dec 31, 2099.

Example:
Dim s as Date
S = Date
Label1 = s

Time
Sets the system time.
Syntax: Time = time
The required time argument is any numeric expression, string expression, or any combination, that can represent a time.
Remarks
If time is a string, Time attempts to convert it to a time using the time separators you specified for your system. If it can’t be converted to a valid time, an error occurs.

Example:
Private Sub Form_Load()
Label1 = Time
End Sub

Now
Returns a Variant (Date) specifying the current date and time according your computer’s system date and time.
Syntax: Now

Example:
Private Sub Form_Load()
Dim s As Date
s = Now
Label1 = s
End Sub
‘Displays the date and time in label1

Timer
Timers execute code repeatedly according to the Interval you specify. Set the Interval property in milliseconds. For example, 2000 = 2 seconds. Timers are useful for checking programs conditions periodically, but don’t get in the habit of using them for everything. A Timer control is not a clock and should not be relied upon to keep accurate time.

DateAdd
Returns a Variant (Date) containing a date to which a specified time interval has been added.
Syntax
DateAdd(interval, number, date)

The DateAdd function syntax has these named arguments:

PartDescription
  
intervalRequired. String expression Required. String expression that is the interval of time you want to Add.
  
numberRequired. Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).
  
dateRequired. Variant (Date) or literal representing date to which the interval is added

Settings
The interval argument has these settings:

SettingDescription
yyyyYear
qQuarter

Settings
The interval argument has these settings:

SettingDescription
yyyyYear
qQuarter
mMonth
yDay of year
dDay
wWeekday
wwWeek
hHour
nMinute
sSecond

Remarks
You can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now. To add days to date, you can use Day of Year (“y”), Day (“d”), or Weekday (“w”).

Example:
DateAdd(“m”, 1, “31-Jan-95”)
In this case, DateAdd returns 28-Feb-95, not 31-Feb-95.
If date is 31-Jan-96, it returns 29-Feb-96 because 1996 is a leap year.

DateDiff
Returns a Variant (Long) specifying the number of time intervals between two specified dates.
Syntax :DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]]) The DateDiff function syntax has these named arguments:

Part Description
intervalRequired. String expression that is the interval of time you use to calculate the difference between date1 and date2
  
date1, date2Required; Variant (Date). Two dates you want to use in the calculation.
  
firstdayofweekOptional.A constant that specifies the first day of the week. If not specified,Sunday is assumed.
  
firstweekofyearOptional A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.
  
firstweekofyearOptional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.

The FirstDayofWeek argument has these settings:

SettingDescription
yyyyYear
mMonth
yDay of year
dDay
hHour
nMinute
sSecond

DateDiff
Returns a Variant (Long) specifying the number of time intervals between two specified dates.

Syntax :DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
The DateDiff function syntax has these named arguments:

PartDescription
intervalRequired. String expression that is the interval of time you use to calculate the Difference between date1 and date2
date1, date2 Required; Variant (Date). Two dates you want to use in the calculation.
firstdayofweekOptional. A constant that specifies the first day of the week. If not specified, Sunday is assumed.
firstweekofyear Optional. A constant that specifies the first week of the year. If not  the first week is assumed to be the week in which January 1 occurs.

Settings: The interval argument has these settings:
Setting Description :-

yyyyYear
qQuarter
mMonth
yDay of year
dDay
wWeekday
wwWeek
hHour
nMinute
sSecond

The firstdayofweek argument has these settings:

ConstantValueDescription
vbUseSystem0Use the NLS API setting.
vbSunday1Sunday (default)
vbMonday2Monday
vbTuesday3Tuesday
vbWednesday 4Wednesday
vbThursday5Thursday
vbFriday6Friday
vbSaturday7Saturday
   
Constant   ValueDescription
vbUseSystem0Use the NLS API setting.
vbFirstJan11Start with week in which January 1occurs (default).
vbFirstFourDays2Start with the first week that has at
vbFirstFullWeek3Start with first full week of the year.

Remarks
You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.

Example:
Dim TheDate As Date ‘ Declare variables.
Dim Msg
TheDate = InputBox(“Enter a date”)
Msg = “Days from today: ” & DateDiff(“d”, Now, TheDate)
MsgBox Msg  ‘Displays difference between dates in number of days

DateSerial
Returns a Variant (Date) for a specified year, month, and day.

Syntax
DateSerial(year, month, day)

PartDescription
yearRequired; Integer. Number between 100 and 9999, inclusive, or a numeric expression.
monthRequired; Integer. Any numeric expression.
dayRequired; Integer. Any numeric expression.

The DateSerial function syntax has these named arguments:

Remarks
To specify a date, such as December 31, 1991, the range of numbers for each DateSerial argument should be in the accepted range for the unit; that is, 1–31 for days and 1–12 for months. However, you can also specify relative dates for each argument using any numeric expression that represents some number of days, months, or years before or after a certain date.

Example:
Dim MyDate
‘ MyDate contains the date for February 12, 1969.
MyDate = DateSerial(1969, 2, 12) ‘ Return a date.

The DateSerial function syntax has these named arguments:

Remarks
To specify a date, such as December 31, 1991, the range of numbers for each DateSerial argument should be in the accepted range for the unit; that is, 1–31 for days and 1–12 for months. However, you can also specify relative dates for each argument using any numeric expression that represents some number of days, months, or years before or after a certain date.

Example:
Dim MyDate
‘ MyDate contains the date for February 12, 1969.
MyDate = DateSerial(1969, 2, 12) ‘ Return a date.

DateValue
Returns a Variant (Date).

Syntax
DateValue(date)

The required date argument is normally a string expression representing a date from January 1, 100 through December 31, 9999. However, date can also be any expression that can represent a date, a time, or both a date and time, in that range.

Remarks
If date is a string that includes only numbers separated by valid date separators, DateValue recognizes the order for month, day, and year according to the Short Date format you specified for your system. DateValue also recognizes unambiguous dates that contain month names, either in long or abbreviated form. For example, in addition to recognizing 12/30/1991 and 12/30/91, DateValue also recognizes December 30, 1991 and Dec 30, 1991.

Example:
Dim MyDate
MyDate = DateValue(“February 12, 1969”) ‘ Returns 12/02/1965

Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?