ghost21blade
ghost21blade

Reputation: 613

Get date difference in VB.NET

I want to get the difference between two dates chosen by two date time pickers in years, months & days in separate text boxes.

I've tried:

txtyrs.text = datediff(datetimepicker1,datetimepicker2)

It is not working.

Upvotes: 17

Views: 102773

Answers (10)

ghost21blade
ghost21blade

Reputation: 613

Thanks everyone who helped.

Some calculations must be done and then,

dim ts as TimeSpan = dtpicker1.value - dtpicker.value
dim days, months, years as integer
months = 12*(dtp1.value.year - dtp2.value.year) (dtp1.value.month-dtp2.value.month)

... More

Upvotes: 0

I've created this function with optional EndDate with default the current one. I've added the zero function to the time.

Public Shared Function Zero(ByVal Number As Integer) As String
    If Number < 10 Then
        Return "0" & Number.ToString
    Else
        Return Number.ToString
    End If
End Function

Public Shared Function TimeDifference(ByVal StartDate As DateTime, Optional ByVal EndDate As DateTime = Nothing) As String
    If EndDate = Nothing Then
        EndDate = Date.Now()
    End If
    Dim timediff As TimeSpan
    If EndDate > StartDate Then
        timediff = EndDate - StartDate
        Return timediff.Days & ":" & Zero(timediff.Hours) & ":" & Zero(timediff.Minutes) & ":" & Zero(timediff.Seconds)
    Else
        timediff = StartDate - EndDate
        Return timediff.Days & ":" & Zero(timediff.Hours) & ":" & Zero(timediff.Minutes) & ":" & Zero(timediff.Seconds)
    End If
End Function

Upvotes: 1

Mohammad Naim Dahee
Mohammad Naim Dahee

Reputation: 939

Using DateDiff, you call it with different date interval parameters to retrieve the appropriate value:

 Dim D1, D2 As Date
    D1 = Date.Now
    D2 = #11/9/2004#
    'DateDiff
    Console.WriteLine("DateDiff")
    Console.WriteLine()
    Console.WriteLine("{0} Days", _
        DateDiff(DateInterval.Day, D1, D2))
    Console.WriteLine("{0} Hours", _
        DateDiff(DateInterval.Hour, D1, D2))
    Console.WriteLine("{0} Minutes", _
        DateDiff(DateInterval.Minute, D1, D2))
    Console.WriteLine("{0} Seconds", _
        DateDiff(DateInterval.Second, D1, D2))
    Console.WriteLine()

Alternatively, a TimeSpan structure can be retrieved as the result of subtracting one date from another, and then querying the various members of that structure.

Console.WriteLine("TimeSpan")
    Console.WriteLine()
    Dim difference As TimeSpan = D2.Subtract(D1)
    Console.WriteLine("{0} Days", difference.TotalDays)
    Console.WriteLine("{0} Hours", difference.TotalHours)
    Console.WriteLine("{0} Minutes", difference.TotalMinutes)
    Console.WriteLine("{0} Seconds", difference.TotalSeconds)
    Console.WriteLine()

The output of the two different methods is nearly identical, except that the TimeSpan properties are returning Doubles, while DateDiff always returns Longs (Int64).

DateDiff

175 Days

4222 Hours

253345 Minutes

15200730 Seconds

TimeSpan

175.934383644387 Days

4222.42520746528 Hours

253345.512447917 Minutes

15200730.746875 Seconds

Upvotes: 4

Manik G
Manik G

Reputation: 341

Use:

datediff(interval, date1, date2);

For example: interval can be day, month, year, hours, second minutes, etc. It subtracts date1 from date2.

Enter date1 and date2 in Dateformat.

Format: DateDiff(DateInterval.Day, Now.Date, Now.AddDays(4).Date)

Output: 4

Upvotes: 11

Muj
Muj

Reputation: 166

I've modified your code so that you can easily understand it.

Try this:

txtyrs.text = DateDiff(DateInterval.Day, datetimepicker1.value,datetimepicker2.value)

I hope this is it. If something wrong or still confusing just inform me.

Upvotes: 2

Sikandar Amla
Sikandar Amla

Reputation: 1475

Dim D1 as Date = Now 
Dim D2 as Date = D1.AddDays(10) 
Dim difference As
TimeSpan = D2.Subtract(D1) 
msgBox(“{0} Days”, difference.TotalDays) 'THIS WILL RETURN Total No. of Days

Upvotes: 2

tinstaafl
tinstaafl

Reputation: 6948

Something like this should work:

    Dim dateOne = DateTimePicker1.Value
    Dim dateTwo = DateTimePicker2.Value
    Dim diff As TimeSpan = dateTwo.Subtract(dateOne)
    Dim years As Double = diff.TotalDays / 365
    txtyrs.Text = years.ToString
    txtmonsh.Text = (years * 12).ToString
    txtdays.Text = diff.TotalDays.ToString

Upvotes: 2

Douglas Barbin
Douglas Barbin

Reputation: 3615

Try this:

txtyrs.text=datediff(DateInterval.Year,datetimepicker1,datetimepicker2).ToString()

Assuming that datetimepicker1 and datetimepicker2 are of type DateTime. If not, you need to get their respective DateTime values and use those instead.

Upvotes: 1

Try this:

Dim date1 As Date = Date.Now
Dim date2 As Date = date1.AddDays(4.0#)

Dim span = date2 - date1

Dim days As Double = span.TotalDays '=4

And if you want to extract the years, take a look at this post.

Upvotes: 15

Tim Schmelter
Tim Schmelter

Reputation: 460108

Use TimeSpan and some date calculaton, this should work:

Dim offset = New Date(1, 1, 1)
Dim dateOne = DateTimePicker1.Value
Dim dateTwo = DateTimePicker2.Value
Dim diff As TimeSpan = dateTwo - dateOne
Dim years = (offset + diff).Year - 1
Dim months = (dateTwo.Month - dateOne.Month) + 12 * (dateTwo.Year - dateOne.Year)
Dim days = diff.Days
TxtYear.Text = years.ToString
TxtMonth.Text = months.ToString
TxtDays.Text = days.ToString

Upvotes: 4

Related Questions