0% found this document useful (0 votes)
1K views

Visual Basic 6 (VB6)

Visual Basic 6 (VB6) is a popular programming language for developing desktop applications. This document provides details on VB6 functions for working with dates and times, including functions to isolate different parts of a date/time value like date, month, year, hour, minute, etc. It also describes functions for combining date/time parts into a single value, and demonstrates their usage with code examples.

Uploaded by

balajiodd
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Visual Basic 6 (VB6)

Visual Basic 6 (VB6) is a popular programming language for developing desktop applications. This document provides details on VB6 functions for working with dates and times, including functions to isolate different parts of a date/time value like date, month, year, hour, minute, etc. It also describes functions for combining date/time parts into a single value, and demonstrates their usage with code examples.

Uploaded by

balajiodd
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 25

Visual Basic 6 (VB6)

Username: *
Password: *

The following functions isolate the date portion and time portion, respectively, of
a Date/Time value:

Function Description
DateValue Returns the date portion of a Date/Time value, with the time
portion "zeroed out". (Note: When the time portion of a date/time
variable is "zeroed out", the time would be interpreted as 12:00
AM.)

Example:

Dim dtmTest As Date

dtmTest = DateValue(Now)

At this point, the date portion of dtmTest is 8/31/2001, with a time


portion of 0 (12:00 AM midnight).

TimeValue Returns the time portion of a Date/Time value, with the date
portion "zeroed out". (Note: When a date/time variable is "zeroed
out", the date will actually be interpreted as December 30, 1899.)

Example:

Dim dtmTest As Date

dtmTest = TimeValue(Now)

At this point, the time portion of dtmTest is 9:15:20 PM, with a


date portion of 0 (12/30/1899).

The following functions are used to isolate a particular part of a date:

Function Description
Weekday Returns a number from 1 to 7 indicating the day of the week for a
given date, where 1 is Sunday and 7 is Saturday.

Example:

intDOW = Weekday(Now) ' intDOW = 6

Note:

When necessary to refer to a day of the week in code, VB has a


set of built-in constants that can be used instead of the hard-
coded values 1 thru 7:

Constant Value
vbSunday 1

vbMonday 2

vbTuesday 3

vbWednesday 4

vbThursday 5

vbFriday 6

vbSaturday 7

Function Description
WeekdayName Returns a string containing the weekday name ("Sunday" thru
"Saturday"), given a numeric argument with the value 1 through
7.

Example:

strDOW = WeekdayName(6) ' strDOW = "Friday"

The WeekdayName function takes an optional, second


argument (Boolean) indicating whether or not to abbreviate the
weekday name. By default, the second argument is False,
meaning do not abbreviate and return the full name. If True, the
first three letters of the weekday name will be returned:

Example:

strDOW = WeekdayName(6, True) ' strDOW = "Fri"


You can nest the Weekday function within the WeekdayName
function to get the weekday name for a given date:

Example:

strDOW = WeekdayName(Weekday(Now)) ' strDOW =


"Friday"
Month Returns a number from 1 to 12 indicating the month portion of a
given date.

Example:

intMonth = Month(Now) ' intMonth = 8

MonthName Returns a string containing the month name ("January" thru


"December"), given a numeric argument with the value 1
through 12.

Example:

strMoName = MonthName(8) ' strMoName = "August"

The MonthName function takes an optional, second argument


(Boolean) indicating whether or not to abbreviate the month
name. By default, the second argument is False, meaning do
not abbreviate and return the full name. If True, the first three
letters of the month name will be returned:

Example:

strMoName = MonthName(8, True) ' strMoName =


"Aug"

You can nest the Month function within the MonthName function
to get the month name for a given date:

Example:

strMoName = MonthName(Month(Now)) ' strMoName =


"August"

Day Returns a number from 1 to 31 indicating the day portion of a


given date.

Example:

intDay = Day(Now) ' intDay = 31

Year Returns a number from 100 to 9999 indicating the year portion
of a given date.

Example:

intYear = Year(Now) ' intYear = 2001

The following functions are used to isolate a particular part of a time:

Function Description
Hour Returns an integer specifying a whole number between 0 and 23
representing the hour of the day.

Example:

intHour = Hour(Now) ' intHour = 21 (for 9 PM)

Minute Returns an integer specifying a whole number between 0 and 59


representing the minute of the hour.

Example:

intMinute = Minute(Now) ' intMinute = 15

Second Returns an integer specifying a whole number between 0 and 59


representing the second of the minute.

Example:

intSecond = Second(Now) ' intSecond = 20

To demonstrate the date functions shown thus far, set up a "Try It" project, and
place the following code in the cmdTryIt_Click event:

Private Sub cmdTryIt_Click()

Print "Now:"; Tab(30); Now

Print "Using DateValue:"; Tab(30); DateValue(Now)

Print "Using TimeValue:"; Tab(30); TimeValue(Now)


Print "Using Weekday:"; Tab(30); Weekday(Now)

Print "Using WeekdayName:"; Tab(30);


WeekdayName(Weekday(Now))

Print "Using WeekdayName (abbrev.):"; Tab(30);


WeekdayName(Weekday(Now), True)

Print "Using Month:"; Tab(30); Month(Now)

Print "Using MonthName:"; Tab(30);


MonthName(Month(Now))

Print "Using MonthName (abbrev.):"; Tab(30);


MonthName(Month(Now), True)

Print "Using Day:"; Tab(30); Day(Now)

Print "Using Year:"; Tab(30); Year(Now)

Print "Using Hour:"; Tab(30); Hour(Now)

Print "Using Minute:"; Tab(30); Minute(Now)

Print "Using Second:"; Tab(30); Second(Now)

End Sub

Run the project and click the "Try It" button. The output should look similar to the
following:
Download the VB project code for the example above here.

The DatePart Function

The generic DatePart function returns an Integer containing the specified part of
a given date/time value. Thus, it incorporates the functionality of the Weekday,
Month, Day, Year, Hour, Minute, and Second functions. In addition, it can used to
get the quarter of a given date (1 through 4) , the "Julian" date (the day of the
year from 1 to 366), and the week number (1 through 53).

Syntax:
DatePart(interval, date[,firstdayofweek[,
firstweekofyear]])

The DatePart function syntax has these parts:

Part Description
interval Required. String expression that is the interval of time you want
to return.

The string expression can be any of the following:

Expression Description Possible Range of Values


"yyyy" Year 100 to 9999
"q" Quarter 1 to 4
"m" Month 1 to 12
"y" Day of year 1 to 366 (a "Julian" date)
"d" Day 1 to 31
"w" Weekday 1 to 7
"ww" Week 1 to 53
"h" Hour 0 to 23
"n" Minute 0 to 59
"s" Second 0 to 59
Date Required. Date value that you want to evaluate.

firstdayofweek Optional. 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 specified, the first week is assumed to be the week in which
January 1 occurs.
To demonstrate DatePart, set up a "Try It" project, and place the following code in
the cmdTryIt_Click event:

Private Sub cmdTryIt_Click()

Print "Current date/time is: "; _

Format$(Now, "Long Date"); _

Spc(1); _

Format$(Now, "Long Time")

Print "*** DatePart Function Examples ***"

Print "Using 'yyyy':"; Tab(20); DatePart("yyyy", Now)

Print "Using 'q':"; Tab(20); DatePart("q", Now)

Print "Using 'm':"; Tab(20); DatePart("m", Now)

Print "Using 'y':"; Tab(20); DatePart("y", Now)

Print "Using 'd':"; Tab(20); DatePart("d", Now)

Print "Using 'w':"; Tab(20); DatePart("w", Now)

Print "Using 'ww':"; Tab(20); DatePart("ww", Now)

Print "Using 'h':"; Tab(20); DatePart("h", Now)

Print "Using 'n':"; Tab(20); DatePart("n", Now)

Print "Using 's':"; Tab(20); DatePart("s", Now)

End Sub
Run the project and click the "Try It" button. The output should look similar to the
following:

Download the VB project code for the example above here.

Piecing Separate Numbers Together to Form a Date or Time Value

In the previous examples, we saw ways to isolate parts of a date/time value.


What if you need to go the "other way"? If you have the separate parts of a
date/time value in different variables and want to piece them together to
formulate a date or time, there are two functions you can use to do this:
DateSerial and TimeSerial.

The DateSerial takes three numeric arguments: year, month, and day
respectively. It returns a date based on those values.

Example:

Dim intYear As Integer

Dim intMonth As Integer

Dim intDay As Integer

Dim dtmNewDate As Date

intYear = 2001

intMonth = 9

intDay = 2

dtmNewDate = DateSerial(intYear, intMonth, intDay)

' returns 9/2/2001

The TimeSerial takes three numeric arguments: hour, minute, and second
respectively. It returns a time based on those values.

Example:

Dim intHour As Integer

Dim intMinute As Integer


Dim intSecond As Integer

Dim dtmNewTime As Date

intHour = 11

intMinute = 34

intSecond = 44

dtmNewTime = TimeSerial(intHour, intMinute, intSecond)

'returns 11:34:44 (AM)

How did you enjoy this article?:


Cancel Rating
20
40
60
80
100

• Printer Friendly
• 91048 reads

Comments
Sat, 07/11/2009 - 15:20 — Need Solutions

How to display the execution time?

Hello Frnds ,

Thanks a lot ... i have learned a lot from this page ....

How can i display the execution time of my tool? ...

i mean if my tool runs for 30 seconds .. at the end it should display 30 sec ... if it runs for
2 mins it should display 2 mins ....
i guess .. i am clear enough.

please have a look into it and reply me back..it is very urgent for my project.

Thanks in advance.

• reply

Sun, 07/12/2009 - 21:39 — Anonymous (not verified)

How to display the execution time?

any updates??

• reply

Fri, 07/10/2009 - 08:35 — ObiekezieC

How do I secure my system Screen

I wrote a cafee clock program to cover my system screen that, if someone want to browse
my system he/she will collect ticket from me and when the time fineshes the program
will block the screen again, the problem now IS HOW (code) TO DISABLE THE
WINDOW KEY BUTTON ON KEYBOARD, THE Ctrl+Alt+Delete and SOME OTHER
SPECIAL KEYS ON KEYBOARD SO THAT WHEN THE FORM LOAD THOSE
KEYS WILL NOT FUNCTION TILL THE USER LOGIN PROPERLY, SO THAT
WHEN THE FORM UNLOADS, IT WILL FUNCTION AGAIN. to prevent an
unauthorize person to login through pressing of window key button or Ctrl+Alt+Delete to
terminate.

Obiekezie

• reply

Fri, 07/03/2009 - 19:52 — winwin pornz (not verified)

2 dtpicker set to first and last day of the current month

Private Sub Form_Load()


DTPicker1.Value = DateAdd("d", -(Day(Date) - 1), Date)
DTPicker2.Value = DateAdd("d", (getNumberOfDays - 1), DTPicker1.Value)
End Sub

'Get the number of days each month is having


Public Function getNumberOfDays() As Integer
Select Case DateTime.Month(Date)
Case 1, 3, 5, 7, 8, 10, 12
getNumberOfDays = 31
Case 4, 6, 9, 11
getNumberOfDays = 30
Case 2
'logic for checking leap years
If (Year(Date) Mod 4) = 0 Then
If (Year(Date) Mod 100) = 0 Then
If (Year(Date) Mod 400) = 0 Then
getNumberOfDays = 29
Else
getNumberOfDays = 28
End If
Else
getNumberOfDays = 29
End If
Else
getNumberOfDays = 28
End If
End Select
End Function

• reply

Sun, 06/28/2009 - 04:46 — Anonymous (not verified)

expiration date and time for 24 hours in vb 6.0

hi,,,plz help me up!//.

how to code date and time expiration.

for example

you are currently register on 01/01/2009 at 09:00 am then

you will expire on 01/02/2009 at 09:00am

display "you are expired"

• reply

Thu, 06/25/2009 - 09:32 — ChekwubeS (not verified)

How do I secure my system Screen


I wrote a cafee clock program to cover my system screen that, if someone want to browse
my system he/she will collect ticket from me and when the time fineshes the program
will block the screen again, the problem now IS HOW (code) TO DISABLE THE
WINDOW KEY BUTTON ON KEYBOARD, THE Ctrl+Alt+Delete and SOME OTHER
SPECIAL KEYS ON KEYBOARD SO THAT WHEN THE FORM LOAD THOSE
KEYS WILL NOT FUNCTION TILL THE USER LOGIN PROPERLY, SO THAT
WHEN THE FORM UNLOADS, IT WILL FUNCTION AGAIN. to prevent an
unauthorize person to login through pressing of window key button or Ctrl+Alt+Delete to
terminate.

• reply

Thu, 06/25/2009 - 09:29 — ChekwubeS (not verified)

How do I secure my system Screen

I wrote a cafee clock program to cover my system screen that, if someone want to browse
my system he/she will collect ticket from me and when the time fineshes the program
will block the screen again, the problem now IS HOW (code) TO DISABLE THE
WINDOW KEY BUTTON ON KEYBOARD, THE Ctrl+Alt+Delete and SOME OTHER
SPECIAL KEYS ON KEYBOARD SO THAT WHEN THE FORM LOAD THOSE
KEYS WILL NOT FUNCTION TILL THE USER LOGIN PROPERLY, SO THAT
WHEN THE FORM UNLOADS, IT WILL FUNCTION AGAIN. to prevent an
unauthorize person to login through pressing of window key button or Ctrl+Alt+Delete to
terminate.

• reply

Wed, 06/17/2009 - 07:18 — rookie1968 (not verified)

Finding the last sunday 00:00AM

Hey guys,

I'm trying to set an integer/date to the last Sunday at midnight that went by. So no matter
when in the week the program is run it will find the variable will be set to the last Sunday
@ 00:00 that went by.

any easy way to do this besides a lot of if statements?

Thanks in advance for your help!

• reply

Wed, 05/06/2009 - 00:42 — FarKabeer (not verified)


Date Range in VB 6.0 to retreive records from Access

Hi guys. I am trying to retrieve records from MS Access Database at a certain Date


Range such as Date Range between 01/01/2009 to 03/01/2009. Depends on the date
range, I should get those records and populate them in a grid or something. I have done
the following code but cannot continue. Also there could be multiple entries for the same
date, example

Invoice # Price Qty Date Total


454633 $12.00 3 01/01/2009 $36.00
454634 $02.00 7 01/01/2009 $14.00
454635 $72.00 3 01/01/2009 $216.00
454636 $30.00 3 01/02/2009 $60.00
454637 $10.00 4 01/03/2009 $40.00
454638 $25.00 2 01/03/2009 $50.00
454639 $25.00 2 01/04/2009 $50.00

Private Sub Command2_Click()


FromDate = DTPicker1.Value ' Assume 01/01/2009
ToDate = DTPicker2.Value ' Assume 01/03/2009
If FromDate = "" Or ToDate = "" Then
MsgBox "Please Select Dates FROM and TO to show more details of the Invoices.",
vbCritical
Else
NumberOfDays = DateDiff("d", FromDate , ToDate) ' Rsult is 89 days
.
.
.
End If
End Sub

• reply

Sun, 04/05/2009 - 16:27 — Anonymous (not verified)

Help

hi guys I want your help in this:


I have 3 Textbox:
the first to show the current date
the second to type a number ,
the third to show the Result from calculate the Current date and the number that I entered

example:
current date:4/21/2008
number: 3
End Date: 4/24/2008

THxxx

• reply

Thu, 04/02/2009 - 00:22 — Anonymous (not verified)

question to print

how do i print data based by date on vb6

• reply

Tue, 03/31/2009 - 16:23 — Anon (not verified)

thanks for the help PROFIT!!

thanks for the help


PROFIT!!

• reply

Fri, 03/27/2009 - 06:23 — Biswarup Bhattacharya (not verified)

Re : Date and Time on Label

Option 1 for VB 2008

Put this in the form_load event


Label1.Text = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now)
Label2.Text = "Time : " + TimeValue(Now)

Put this in the timer1_tick event


Label1.Text = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now)
Label2.Text = "Time : " + TimeValue(Now)

Option 2 for VB 2008

Put this in the form_load event


Label1.Text = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now) + " "
+ "Time : " + TimeValue(Now)
Put this in the timer1_tick event
Label1.Text = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now) + " "
+ "Time : " + TimeValue(Now)

Option 1 for VB 6

Put this in the form_load event


Label1.Caption = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now)
Label2.Caption = "Time : " + TimeValue(Now)

Put this in the timer1_timer event


Label1.Caption = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now)
Label2.Caption = "Time : " + TimeValue(Now)

Option 2 for VB 6

Put this in the form_load event


Label1.Caption = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now) +
" " + "Time : " + TimeValue(Now)

Put this in the timer1_timer event


Label1.Caption = "Date : " + WeekdayName(Weekday(Now)) + " " + DateValue(Now) +
" " + "Time : " + TimeValue(Now)

Thanks.

• reply

Thu, 03/19/2009 - 20:15 — Anonymous (not verified)

date and time on label

How can I place the date and time on a label??

• reply

Sun, 06/28/2009 - 03:07 — Warrior8 (not verified)

Good Luck

example: lblLabel.Caption = Now

• reply

Mon, 03/09/2009 - 23:42 — Ramkumar K (not verified)


how to declare monthname(i) in visual basic 5.0?

i am getting the error "Compile error: Sub or function not defined" in this code. I checked
the references needed for this code. Every references are checked. But still it's showing
the same error. It's showing the error in the monthname area. Can someone help me to get
rid of this problem. The code is given below.

Private Sub FillMonths(cboMonths As ComboBox)


Dim i As Integer
Dim strMonth As String
With cboMonths
For i = 1 To 12
strMonth = monthname(i)
.AddItem strMonth
.ItemData(cboMonths.NewIndex) = i
Next i
End With
End Sub

• reply

Sun, 03/08/2009 - 23:48 — karthik (not verified)

date

HAI

How to find days between two dates in vb 6.0(dates from dtpicker)

• reply

Thu, 02/26/2009 - 05:35 — Trevor (not verified)

Scheduling programs to run at given time on given day

I wonder if you are able to help please?

I am considering writing a VB6 program to sync and/or backup folders/files selected as a


way of learning more about VB6.

I'm not sure how to schedule the program to start at the scheduled time and day from
within the coding.

With thanks for any help you may be able to give me to get started.
Trevor

• reply
Wed, 02/25/2009 - 20:34 — Anonymous (not verified)

how do you calculate the

how do you calculate the date of easter

• reply

Wed, 02/18/2009 - 00:46 — Anonymous (not verified)

searching the database

how to search the database in visual basic 6?

• reply

Wed, 02/18/2009 - 00:44 — Anonymous (not verified)

date/time picker

how will i compute age when i use the date/time picker? thanks....

• reply

Tue, 02/17/2009 - 22:26 — Anonymous (not verified)

Storing Server's Date in Database

Please tell me how to store servers date in oracle database using visual basic 6.0 code.

• reply

Sat, 02/14/2009 - 12:00 — Anonymous (not verified)

VB Mondays Dates between a range into Access table

Hi,

I am trying to populate an access database table named DateList that will be used to
populate dropdown lists. I want to write code that, from a web page form, will take a
"from" date and a "through" date and populate the table's (only) field Date with dates
between and including from and through.
I only want the date of MONDAY of each week between the two dates.

This is for an online task management tool that I am creating for my department at work.
The reason I need to code this is that other depts will use this as well and each year we
want to clear and repopulate the dates and I have written the code to clear the table but
now need to repopulate it.l

One of my main concerns here is how to determine how to count the days so that I get
Monday's date and increment so that
I do not get say May 35th. Is there a function that keeps track of the actual date so that
once I determine the first Monday in the range I can use something like Date() + 7?

Thanks
W.

• reply

Tue, 02/10/2009 - 13:49 — Anonymouse (not verified)

question

i am using a db with my vb6 project.


how do i search for records stored on db by searching by date on vb6??

• reply

Wed, 02/11/2009 - 11:35 — krentoX (not verified)

db... VB6.. searching by date

try this:

Dim sql As String

sql = "SELECT vData1, vData2, dDate FROM table WHERE Day(dDate) = " &
Day(TDBDate.Value) & " AND Month(dDate) = " & Month(TDBDate.Value) & " AND
Year(dDate) = " & Year(TDBDate.Value) Order By dDate"

Set rs = New ADODB.Recordset

rs.CursorLocation = 3
rs.LockType = 1
rs.CacheSize = 1
rs.CursorType = 3
rs.Open sql, ConexionString, , , 1
-------------------
DataCombo1.Text = rs("vData1").Value
TextBox1.Text = rs("vData2").Value
ConexionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User
ID=USERDB;Initial Catalog=DataBase;Data Source=Server;Use Procedure for
Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=PC;Use Encryption
for Data=False;Tag with column collat"

---I hope clarified your question-----

• reply

Mon, 02/02/2009 - 06:47 — Chaminda Senarathne (not verified)

Viewed and thanks

Thanks very much for this page and helped and saved lot of time of me in finding some
solutions

• reply

Thu, 01/29/2009 - 00:26 — shiba (not verified)

biometric code in vb

good day please help me how to do a system payroll when the in and out of employee is
come in biometric what code can i put???????????????........................the name of
biometric i use is ACROPRINT TIME qPLUS BIOMETRIC >>>
THANKS>>>>>>>>>>>>>>>>>>

• reply

Wed, 01/21/2009 - 10:44 — Anonymous (not verified)

How do I increase a date in

How do I increase a date in cell E3 (excel) by one week using VB

• reply

Fri, 01/09/2009 - 09:24 — Anonymous (not verified)

thank you...

thank you....

• reply

Sat, 11/01/2008 - 09:29 — Lenard (not verified)


Solving time and date

I can't see the answer please post it the exact answer for example I want to add 1 hr. in
current time how can i do that

• reply

Tue, 12/09/2008 - 20:43 — Anonymous (not verified)

dim currenttime as

dim currenttime as string


currenttime = dateadd("h",1,now)

• reply

Sat, 11/01/2008 - 09:25 — Lenard (not verified)

Solving time and date

in solving date I use "dateadd("d",30,date) or datediff("d",30,date)" but how to solve the


time??? please help me e-mail for information tnx [email protected]

• reply

Mon, 01/21/2008 - 21:29 — Anonymous

try this....date and time for 24hours and 12hours....

http://www.vb6.us/tutorials/date-time-functions-visual-basic

• reply

Post new comment


Your name:
E-mail:
The content of this field is kept private and will not be shown publicly.
Homepage:
Subject:
Comment: *

• Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl>
<dt> <dd>
• Lines and paragraphs break automatically.
• You may post block code using <blockcode [type="language"]>...</blockcode>
tags. You may also post inline code using <code [type="language"]>...</code>
tags.

More information about formatting options

You might also find these useful:


• Learn howto use the Visual Basic DateDiff function
• Understanding VB6's DateAdd function
• Creating PDF files in Visual Basic
• Make a Club Penguin Trainer
• Convert C Strings to VB Strings
• Activate Any Window With API
• VB6 Programming Standards
• VB6 Naming Conventions
• Naming Database Objects
• Access SQL

You might also like