By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

Subtracting Dates

P: n/a
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?
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Amjad" <Am***@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
|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?
Nov 21 '05 #2

P: n/a
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

"Amjad" <Am***@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
|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?

Nov 21 '05 #3

P: n/a
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

"Amjad" <Am***@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
| 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
| >
| >
| >
| > "Amjad" <Am***@discussions.microsoft.com> wrote in message
| > news:5E**********************************@microsof t.com...
| > |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?
| >
| >
| >
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.