ARTICLE

Add and subtract functionalities in DateTime and VB.NET

Posted by Hirendra Sisodiya Articles | Visual Basic 2010 March 19, 2010
Add and subtract functionalities in DateTime and VB.NET
 
Reader Level:

Calculating  DateTime Difference or interval

Suppose that you have two date time values one is startdate and second is EndDate, and you want to calculate time interval between both dates ,in this situation we can use timespan object like this:

(According to MSDN:  A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the DateTime or DateTimeOffset  structure should be used instead.)

 

        Dim StartTime As DateTime = #6/12/2008 3:09:00 PM#

        Dim EndTime As DateTime = #6/10/2008 12:04:00 PM#

        Dim TimeDiff As TimeSpan = EndTime - StartTime

 

        Dim Days As Integer = TimeDiff.Days                 'calculate Value of Days Component

        Dim Minutes As Integer = TimeDiff.Minutes           'calculate Value of Minutes Component

        Dim Seconds As Integer = TimeDiff.Seconds           'calculate Value of Seconds Component

        Dim TotalDays As Integer = TimeDiff.TotalDays       'calculate Value of Total Days

        Dim TotalHours As Integer = TimeDiff.TotalHours     'calculate Value of Total Minutes

        Dim TotalSeconds As Integer = TimeDiff.TotalSeconds 'calculate Value of Total Seconds

 

        we can use subtrat method for calculating difference between two date like :

 

        TimeDiff = EndTime.Subtract(StartTime)

 

Add a day, a month and a year to a Date

 

            Dim FirstDate As Date

            Dim SecondDate As Date

 

            FirstDate = #2/28/2010#

            'Add a day

            SecondDate = FirstDate.AddDays(1)

            'Add some months

            SecondDate = FirstDate.AddMonths(6)

            'Subtract a year

            SecondDate = FirstDate.AddYears(-1)

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    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.
Become a Sponsor