Handling Dates and Times in VB.net

By: Steven Holzner Emailed: 1700 times Printed: 2205 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

One of the biggest headaches a programmer can have is working with dates. Handling hours, minutes, and seconds can be as bad as working with shillings, pence, and pounds. Fortunately, Visual Basic has a number of date and time handling functions, which appear in Table below-you can even add or subtract dates using those functions. VB6 programmers will notice a number of new properties in this table.

Visual Basic date and time properties.

To do this

Use this

Get the current date or time

Today, Now, TimeofDay, DateString, TimeString

Perform date calculations

DateAdd, DateDiff, DatePart

Return a date

DateSerial, DateValue

Return a time

TimeSerial, TimeValue

Set the date or time

Today, TimeofDay

Time a process

Timer

Here's an example in which I'm adding 22 months to 12/31/2001 using DateAdd-you might note in particular that you can assign dates of the format 12/31/2001 to variables of the Date type if you enclose them inside # symbols:

Imports System.Math
Module Module1
    Sub Main()
        Dim FirstDate As Date
        FirstDate = #12/31/2001#
        System.Console.WriteLine("New date: " & DateAdd_
        (DateInterval.Month, 22, FirstDate))
    End Sub
End Module

Here's what you see when you run this code:

New date: 10/31/2003
Press any key to continue

There's something else you should know-the Format function makes it easy to format dates into strings, including times. For easy reference, see Table 2.11, which shows some ways to display the date and time in a string-note how many ways there are to do this.

Table 2.11: Using Format to display dates and times.

Format Expression

Yields this

Format(Now, "M-d-yy")

"1-1-03"

Format(Now, "M/d/yy")

"1/1/03"

Format(Now, "MM - dd - yy")

"01 /01 / 03"

Format(Now, "ddd, MMMM d, yyy")

"Friday, January 1, 2003"

Format(Now, "d MMM, yyy")

"1 Jan, 2003"

Format(Now, "hh:mm:ss MM/dd/yy")

"01:00:00 01/01/03"

Format(Now, "hh:mm:ss tt MM-dd-yy")

"01:00:00 AM 01-01-03"

You can also compare dates and times directly. For example, here's how you loop until the current time (returned as a string by TimeString) exceeds a certain time; when the time is up, the code beeps using the Visual Basic Beep function:

While TimeString < "15:45:00"
End While
Beep()
Tip 

Don't use the above code snippet for more than an example of how to compare times! The eternal looping while waiting for something to happen is a bad idea in Windows, because your program monopolizes a lot of resources that way. Instead, set up a Visual Basic Timer and have a procedure called, say, every second.


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Handling Timer Events - and Creating an Alarm Clock in VB.net
How to export from DataGridView to excel using VB.net
Handling Dates and Times in VB.net
How To Connect To A MySql Database in VB.net
String Insert, index off & format in VB.net
File stream operations in VB.net
To read the entire worksheet in an Excel workbook through VB.net Code
Insert Background Pictures in Excel 2007 from VB.NET
Read Data from Excel using OLEDB in VB.NET 2005
Sending email using CDOSYS in VB.net
Chat client in VB.net
Using a Web Service in VB.net
Structured Exception Handling in VB.net
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
Most Emailed Articles (in VB.net)
Procedure Delegates in VB.net
Chat Server in VB.net
How To Connect To A MySql Database in VB.net
File operations in VB.net
Excel 2007 Data Validation Input Message using VB.net
How to export from database to excel using VB.net
How to export from DataGridView to excel using VB.net
Multi threaded Socket Programming in VB.net
XML and VB.net
How to create an XML file in VB.net
A tutorial on Chat Server and Chat Client in VB.net
Send SMS using VB code
Creating Menus in Code using VB.net
Creating a Web Service in VB.net
Structured Exception Handling in VB.net