# How to do date time calculations

• ### Question

• User-79313405 posted

Hi there,

i have two date time values (dd/mm/yyyy) <== This comes from the database
the second one come from the NOW function in VB, also in the form of (dd/mm/yyyy)

how do i compare the two values to find the difference in the time, eg (1 day and 4 hours)

thanks,
Vidhu

Saturday, January 23, 2010 12:12 PM

• User-925904572 posted

try this

```Dim d1 As Date = Now()
Dim d2 As Date = new Date(2010, 3, 27)

Dim ts As TimeSpan = (d2 - d1)```

now you can use the TimeSpan properties such as Days, Minutes, ect...

let me know if this helped

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Saturday, January 23, 2010 1:56 PM
• User-952121411 posted

Yes here is a way to get the full output as you want:

```        'Parse the DataBase date into a date
Dim DbDate As Date
DbDate = Date.Parse("01/15/2010")

'Create a variable that holds the current date
Dim CurrentDay As Date
CurrentDay = Now()

'Get total seconds between dates
Dim SecondsDiff As Long = DateDiff(DateInterval.Second, DbDate, CurrentDay)
'Get full hours between dates
Dim HoursDiff As Long = SecondsDiff \ 3600
'Get full remaining minutes between dates
Dim MinutesDiff As Long = SecondsDiff \ 60 Mod 60
'Get full remaining seconds in dates
SecondsDiff = SecondsDiff Mod 60

Dim TimeDifference As String = HoursDiff & " Hours " & MinutesDiff & " Minutes " & SecondsDiff & " Seconds"```

...and here is the final output:

"281 Hours 15 Minutes 29 Seconds"

You can do any variation needed like changing hours to days, etc with the code above.

Hope this helps!

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Tuesday, January 26, 2010 5:18 PM

### All replies

• User-925904572 posted

try this

```Dim d1 As Date = Now()
Dim d2 As Date = new Date(2010, 3, 27)

Dim ts As TimeSpan = (d2 - d1)```

now you can use the TimeSpan properties such as Days, Minutes, ect...

let me know if this helped

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Saturday, January 23, 2010 1:56 PM
• User-952121411 posted

Yes here is a way to get the full output as you want:

```        'Parse the DataBase date into a date
Dim DbDate As Date
DbDate = Date.Parse("01/15/2010")

'Create a variable that holds the current date
Dim CurrentDay As Date
CurrentDay = Now()

'Get total seconds between dates
Dim SecondsDiff As Long = DateDiff(DateInterval.Second, DbDate, CurrentDay)
'Get full hours between dates
Dim HoursDiff As Long = SecondsDiff \ 3600
'Get full remaining minutes between dates
Dim MinutesDiff As Long = SecondsDiff \ 60 Mod 60
'Get full remaining seconds in dates
SecondsDiff = SecondsDiff Mod 60

Dim TimeDifference As String = HoursDiff & " Hours " & MinutesDiff & " Minutes " & SecondsDiff & " Seconds"```

...and here is the final output:

"281 Hours 15 Minutes 29 Seconds"

You can do any variation needed like changing hours to days, etc with the code above.

Hope this helps!

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Tuesday, January 26, 2010 5:18 PM
• User-79313405 posted

Greetings atconway,

thanks for your reply since this also helped my to achieve what i wanted :D
but could u add the dayDiff, months diff and year diff

Regards,
Vidhu

Friday, January 29, 2010 5:59 AM