O'Reilly logo

Visual Basic 2005 Cookbook by Tim Patrick, John Clark Craig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required