Time calculation

  • Thread starter Thread starter John
  • Start date Start date
J

John

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
 
* "John said:
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)
///
 
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
 
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
///
 
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 said:
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
 
Back
Top