468,119 Members | 1,972 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Time calculation

Hi

I have a start date and a start time field. I also have a timespan value
which can be anything from 5 minutes (TimeSpan(0, 5, 0)) to 2 weeks
(TimeSpan(14, 0, 0, 0)). How can I get an end date and end time (separate
fields) by adding timespan to start date & start time?

Thanks

Regards
Nov 20 '05 #1
4 2183
* "John" <jo**@nospam.infovis.co.uk> scripsit:
I have a start date and a start time field. I also have a timespan value
which can be anything from 5 minutes (TimeSpan(0, 5, 0)) to 2 weeks
(TimeSpan(14, 0, 0, 0)). How can I get an end date and end time (separate
fields) by adding timespan to start date & start time?


\\\
Dim ts As TimeSpan = New TimeSpan(5, 0, 0, 0)
Dim dtm As Date = DateTime.Now
dtm = Date.op_Addition(dtm, ts)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
Hi

In my case the date and time are separate fields like that of an outlook new
appointment. So e.g startdate="18/07/2004" and start time = "13.00". Now I
need to add various time spans and get enddate & endtime as separate fields.

Thanks

Regards

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eJ*************@TK2MSFTNGP09.phx.gbl...
* "John" <jo**@nospam.infovis.co.uk> scripsit:
I have a start date and a start time field. I also have a timespan value
which can be anything from 5 minutes (TimeSpan(0, 5, 0)) to 2 weeks
(TimeSpan(14, 0, 0, 0)). How can I get an end date and end time (separate fields) by adding timespan to start date & start time?


\\\
Dim ts As TimeSpan = New TimeSpan(5, 0, 0, 0)
Dim dtm As Date = DateTime.Now
dtm = Date.op_Addition(dtm, ts)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #3
Hi John,

Does this question I once answered with a sample I made help you.

It is the calculation of an avarage time, however I think that all your
questions are in it.
\\\
Module main
Public Sub main()
Dim startdates() As String = _
{"01/01/2004 12:50", "02/01/2004 13:40", "02/01/2004 14:30"}
Dim enddates() As String = _
{"02/01/2004 18:40", "02/01/2004 13:57", "02/01/2004 19:50"}
Dim span(2) As TimeSpan
Dim totalspan As TimeSpan
For i As Integer = 0 To 2
span(i) = CDate(enddates(i)).Subtract(CDate(startdates(i)))
totalspan = totalspan.Add(span(i))
Next
span.Sort(span)
MessageBox.Show("min = " & span(0).ToString & vbCrLf & _
"max = " & span(2).ToString & vbCrLf & _
"avarage = " & totalspan.FromSeconds _
(totalspan.TotalSeconds / 3).ToString())
End Sub
End Module
///
Nov 20 '05 #4
John,
In addition to the other comments.

Hint: Outlook actually stores a single field, but displays it with two
controls!

I would have a startDateTime, add the TimeSpan, to get the new
startDateTime.

Dim startDateTime As DateTime = #7/1/2004 10:00:00 AM#

Dim duration As New TimeSpan(7, 5, 0, 0)

startDateTime = startDateTime.Add(duration)

I would bind this single startDateTime to a StartDateControl & a
StartTimeControl using the formatting previously given so each control only
displays its part, taking into consideration the "other half" when I put the
values back together...

Alternatively If I really needed both StartDate & StartTime fields, I would
combine the two fields, add the duration, then split the sum. However I
would look at using a single field first!

Dim startDate As DateTime = #7/1/2004#
Dim startTime As DateTime = #10:00:00 AM#
Dim duration As New TimeSpan(7, 5, 0, 0)

Dim temp As DateTime =
startDate.Add(startTime.TimeOfDay).Add(duration)

startDate = temp.Date
startTime = DateTime.MinValue.Add(temp.TimeOfDay)

Or I may use a combination of the above, for example my Domain Object may
have a single startDateTime field, but a StartDate & a StartTime property,
where the properties operate on their respective half of the startDateTime
field...

Hope this helps
Jay
"John" <jo**@nospam.infovis.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi

In my case the date and time are separate fields like that of an outlook new appointment. So e.g startdate="18/07/2004" and start time = "13.00". Now I
need to add various time spans and get enddate & endtime as separate fields.
Thanks

Regards

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eJ*************@TK2MSFTNGP09.phx.gbl...
* "John" <jo**@nospam.infovis.co.uk> scripsit:
I have a start date and a start time field. I also have a timespan value which can be anything from 5 minutes (TimeSpan(0, 5, 0)) to 2 weeks
(TimeSpan(14, 0, 0, 0)). How can I get an end date and end time (separate fields) by adding timespan to start date & start time?


\\\
Dim ts As TimeSpan = New TimeSpan(5, 0, 0, 0)
Dim dtm As Date = DateTime.Now
dtm = Date.op_Addition(dtm, ts)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tom | last post: by
10 posts views Thread by Drum2001 | last post: by
8 posts views Thread by andreas | last post: by
5 posts views Thread by The alMIGHTY N | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.