LESSON4 - Data - Date - Formart - Functions
LESSON4 - Data - Date - Formart - Functions
When you’re working with many variables of different data types in an expression, the variable or expression on the right
hand side of the assignment operator must first be converted into the target type. Otherwise assignment will not be
possible. In some cases, Visual Basic does it for you. That means that you don’t need to write code to convert the data
types conversion is done automatically. But this doesn’t happen all the time
Conversion functions
The functions are CBool, CByte, CCur, CDate, CDbl, CDec, CInt, CLng, CSng, CStr, and CVar.
Val and Str functions are also used.
1. CStr: The Cstr function converts an expression to a string. This is useful when you’re displaying some numeric
values in a text box control.
Example:
Dim v1 As Double
Text1.Text = CStr(v1)
2. Str : The Str function converts a number to a string. It is similar to the Cstr function.
Example:
Dim str1 As String, m As Integer
str1 = Str(m)
3. CDbl: The CDbl function converts an expression into a Double. It also extracts the numeric value from a string.
Example: If you omit the CDbl function, the program will raise an overflow error.
Example:
Dim value As Double
value = CDbl(Text1.Text)
Print value + 1
Example:
Dim value As Double
value = CDbl("55")
Print value + 1
4. Val: Sometimes you can use the Val function instead of the CDbl function. Val function returns a Double value.
It only returns the numeric value contained in a string.
Example:
Dim l As Double
l = Val(Text1.Text)
5. CInt: The CInt function converts a number to integer.
Example:
Dim m As Integer
m = CInt(876.878) 'Returns 877 after roundin the no.
m = CInt(-2.7) 'Returns -3 after rounding the no.
Dim B as Boolean
B=cbool(0) 'Returns False
B=cbool(1) 'Returns true
B=cbool(345) 'Returns True
NB: CByte, CSng, CVar,CCur,CDate and CDecimal functions convert to Byte, Sinle, Variant, Currency, Date and
Decimal values respectively. You can use these functions in the same way explained above.
Formatting Functions
Formatting output is an important part of programming so that the visual interface can be presented clearly to the users.
Data in the previous lesson were presented fairly systematically through the use of commas and some of the functions like
Int, Fix and Round. However, to better present the output, we can use a number of formatting functions in Visual basic.
The three most common formatting functions in VB are Tab, Space, and Format
Example1
Example 12.3
Example 12.4
Private Sub Form_Activate()
Print Format(781234.57, "0")
Print Format(781234.57, "0.0")
Print Format(781234.576, "0.00")
Print Format(781234.576, "#,##0.00")
Print Format(781234.576, "$#,##0.00")
Print Format(0.576, "0%")
Print Format(0.5768, "0.00%")
End Sub
Date And Time Functions
Recall the VB keywords that reference the current date and/or time:
Now A vb6 date function that returns the current date and time together
Date Returns the current date
Time Returns the current time
For the examples that follow, assume that the current Date/Time (Now) is Friday, August 31, 2001at 9:15:20 PM.
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 vb6 time portion of a date/time variable is "zeroed out", the time would be
interpreted as 12:00 AM.)
Example:
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:
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 vb6 date:
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"
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
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:
End Sub
Run the project and click the "Try It" button. The output should look similar to the following:
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]])
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:
"q" Quarter 1 to 4
"m" Month 1 to 12
"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
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:
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.
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:
intYear = 2001
intMonth = 9
intDay = 2
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