Chapter 7. Dates and Times

Introduction

The Date data type holds a compact representation of an exact moment in time, with 100-nanosecond resolution, covering a 10,000-year span of dates starting with day 1 of year 1 AD. Visual Basic 2005 and the .NET Framework provide many powerful functions for working with dates and times, but the syntax and conceptual changes can be daunting, especially if you’re updating your skills from VB 6. It can often be tricky to figure out how or what to use to get the job done.

The good news is that once you get up to speed with all the changes, it’s now easier than ever to work with dates and times. Is a given year a leap year? How many days are there in a given month? What day of the year is a given date? All of these questions, and many more, can now be answered with single function calls or single lines of code.

The recipes in this chapter provide solutions for many of the common date and time calculations that come up in day-to-day development, and they should get you up to speed in “no time” (pun intended).

7.1. Getting the System Date and Time

Problem

You want to know the current time and date.

Solution

Sample code folder: Chapter 07\SystemDateTime

Use Now, which returns the current moment of time from your system clock as a Date value.

Discussion

The Now property returns a Date, which you can store in a Date variable or process directly. There are many properties and methods available to extract information from Dates. The following code demonstrates ...

Get Visual Basic 2005 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.