Discovering Visual Basic .NET: Using Functions and Arguments, Page 5
Date and Time Functions
These VB.NET functions provide key date and time access and manipulation functions.
Today, TimeOfDay, Now
Today, TimeOfDay, and Now are functions that take no arguments and each returns a single value, swiped from the system clock:
- Today returns today's date.
- TimeOfDay returns the current time.
- Now returns the date and time together.
Weekday, WeekdayName
Weekday accepts a date as an argument. It returns a number between 1 and 7, indicating the number of the day on which that date falls.
WeekdayName conveniently accepts a number argument between 1 and 7 and returns the name of the associated weekday:
WeekdayName(Weekday(Today))
This line would display the current day of the week.
Other date and time functions
VB.NET provides a broad variety of date and time commands and functions. Here are some date and time functions you may find interesting:
- DateDiff: Returns how many days, weeks, months, or years exist between two dates.
- DateAdd: Adds a certain number of days, weeks, months, or years to a date and returns the new date.
- Day, Month, Year: Each of these functions takes a date and returns a number, which indicates the part of the date associated with the function's name. For example, if MyDate holds 10/5/67, then Month(MyDate) returns 10, Day(MyDate) returns 5, and Year(MyDate) returns 67.
- MonthName: Takes a month number (1 to 12) and returns the name of the month ("January" through "December").
Summary
In this installment, you discovered statements and functions and how they work in a Visual Basic program. You also explored a host of handy built-in functions that can make your programs more powerful, user-friendly and useful. In the next article, I'll show you how to make your programs smarter by letting them make their own decisions.
About the Author
Bill Hatfield (MCSD, MCAD, MCP) is an internationally best-selling author of books on Internet, intranet and client/server technologies. Among his most recent books is ASP.NET For Dummies.
# # #
