473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Subtracting Dates

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
3 1981
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jesse O | last post by:
I have two date fields, start_date and end_date. I'd like to subtract the two dates, and come up with a number (the number of difference between the two dates). What function is there to do...
2
by: Eddie | last post by:
When I subtract dates, I can't figure out what I get out. I first I thought it was in seconds, but that's not it. Then I figured maybe HHMMSS, but that does not seem to be it too. How can I...
6
by: Brian Henry | last post by:
How would i take two dates startdate and enddate and subtract startdate from enddate to figure the number of days between the two? thanks
9
by: Mike Fellows | last post by:
i have 2 dates one is todays date and the other is a future date i want to take todays date from ther future date and tell me how many days difference this is thanks in advance Mike...
8
by: Ifollowhim | last post by:
I have a (installation) date in a table that I put in manually. It is the date we want to complete a job. In a form that uses data from that table I want to add a text box that is bound to the...
4
by: David S. Alexander | last post by:
How can I do simple subtraction in an XSLT. I want to read a few attribute values from an XML document, calculate their difference, and transform that value to an attribute in the XML output...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory area obtained with malloc(). Assume p1 = p2 + some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.