Subtracting Dates

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to subtract two Date variables and show the result in this format:
hh:mm:ss

I'm thinking that there might be an easy way of doing that. something like:
....
Return DateDiff(DateInterval.Second, date1, date2).ToString("hh:mm:ss")
....

Is there such an easy way of doing this?
 
Amjad
I normally use DateTime.Subtract to subtract dates:

Dim date1 As DateTime = #10:30:00 AM#
Dim date2 As DateTime = #9:30:00 AM#
Dim ts As TimeSpan = date1.Subtract(date2)

To get to hh:mm:ss specifically (or other custom date/time formats) I
normally convert the TimeSpan to a date.

Dim r As DateTime = DateTime.MinValue.Add(ts)
Dim s As String = r.ToString("hh:mm:ss")

TimeSpan.ToString will optionally include any days & fraction of seconds in
the converted value.

NOTE: You need to make sure the TimeSpan is positive for the
DateTime.MinValue.Add method to work.

Hope this helps
Jay



|I want to subtract two Date variables and show the result in this format:
| hh:mm:ss
|
| I'm thinking that there might be an easy way of doing that. something
like:
| ...
| Return DateDiff(DateInterval.Second, date1, date2).ToString("hh:mm:ss")
| ...
|
| Is there such an easy way of doing this?
 
Thanks Jay! That answered my question.

I have a follow-up question, and that is how can I add up the time periods
of the format "HH:mm:ss" and report the result in the same format?
 
Amjad,
Have you tried using TimeSpan.Add to keep a running total of "time"?

Dim date1 As DateTime = #12:00:00 PM#
Dim date2 As DateTime = #8:00:00 AM#
Dim ts1 As TimeSpan = date1.Subtract(date2)

Dim date3 As DateTime = #5:00:00 PM#
Dim date4 As DateTime = #1:00:00 PM#
Dim ts2 As TimeSpan = date3.Subtract(date4)

Dim total As TimeSpan = ts1.Add(ts2)


Then convert the total to a string.

Dim r As DateTime = DateTime.MinValue.Add(total)
Dim s As String = r.ToString("hh:mm:ss")

Hope this helps
Jay

| Thanks Jay! That answered my question.
|
| I have a follow-up question, and that is how can I add up the time periods
| of the format "HH:mm:ss" and report the result in the same format?
|
|
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > Amjad
| > I normally use DateTime.Subtract to subtract dates:
| >
| > Dim date1 As DateTime = #10:30:00 AM#
| > Dim date2 As DateTime = #9:30:00 AM#
| > Dim ts As TimeSpan = date1.Subtract(date2)
| >
| > To get to hh:mm:ss specifically (or other custom date/time formats) I
| > normally convert the TimeSpan to a date.
| >
| > Dim r As DateTime = DateTime.MinValue.Add(ts)
| > Dim s As String = r.ToString("hh:mm:ss")
| >
| > TimeSpan.ToString will optionally include any days & fraction of seconds
in
| > the converted value.
| >
| > NOTE: You need to make sure the TimeSpan is positive for the
| > DateTime.MinValue.Add method to work.
| >
| > Hope this helps
| > Jay
| >
| >
| >
| > | > |I want to subtract two Date variables and show the result in this
format:
| > | hh:mm:ss
| > |
| > | I'm thinking that there might be an easy way of doing that. something
| > like:
| > | ...
| > | Return DateDiff(DateInterval.Second, date1,
date2).ToString("hh:mm:ss")
| > | ...
| > |
| > | Is there such an easy way of doing this?
| >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top