ARTICLE

Working with Date type in vb.net

Posted by Sapna Articles | Visual Basic Language July 11, 2011
Date Type use to represent date and time.
Download Files:
 
Reader Level:

Date type is  used to represent date, time and both date and a time with values ranging from 12:00:00 midnight, January 1, 0001 C.E. (Common Era) to 11:59:59 P.M., December 31, 9999 C.E. .  Date part represents only date, month and year between 1/1/1 to 31/12/9999. Time part represents time of a day in millisecond unit. Time value can be either negative or positive.

Code:

Module Module1
    Sub Main()
        Dim d1 As New Date(2010, 7, 11)
        Console.WriteLine("Date Time 1 : {0}", d1)

        Console.WriteLine()Date TypeDate Type

        'Date time using String Format.
        Console.WriteLine("Date Time 1 without time: {0:d}", d1)
        Console.WriteLine()

        'Date time using String Format.         
        Dim format As String = [String].Format("{0:d}", d1)
        Console.WriteLine("Date Time 1 without time: {0}", format)
        Console.WriteLine()

        Dim d2 As New Date(2010, 7, 11, 12, 30, 45)
        Console.WriteLine("DateTime 2: {0}", d2)
        Console.WriteLine()

        Dim d3 As New Date(2010, 7, 11, 12, 30, 45,10)
        Console.WriteLine("DateTime 3: {0}", d3)
        Console.WriteLine()

        Dim d4 As New Date(2010, 7, 11)
        Console.WriteLine("dt1: {0}", d4)
        Console.WriteLine()

        Dim Yesterday As Date = GetYesterdayDateTime()
        Console.WriteLine("Yesterday: {0}", Yesterday)
        Console.WriteLine()

        Dim Morrow As Date = GetTomorrowDateTime()
        Console.WriteLine("Tomorrow: {0}", Morrow)
        Console.ReadLine()
    End Sub
    Private Function GetYesterdayDateTime() As Date
        Return Date.Today.AddDays(-1)
    End Function
    Private Function GetTomorrowDateTime() As Date
        Return Date.Today.AddDays(1)
    End Function

End
Module

Output


Output.gif

Date Type
  

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor