O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

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

Get the System Date and Time

Problem

You need to retrieve the current date and time.

Solution

Use the DateTime.Now shared property.

Discussion

DateTime.Now retrieves the current date and time as a DateTime structure. You can then retrieve specific date information from its properties, such as Month, Day, Date, Hour, Millisecond, and even DayOfWeek.

Dim Now As DateTime = DateTime.Now

Console.WriteLine("The current date is: " & Now.ToString())
Console.WriteLine("It's a " & Now.DayOfWeek.ToString())

The output for this code is as follows:

The current date is: 2002-10-31 5:24:14 PM
It's a Thursday

If you want to retrieve only the date portion of the current day, you can use the DateTime.Today shared method. The time portion will be set to 12:00 AM (00:00:00). ...

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