Jump to content

Calculate duration between time values


mierlp

Recommended Posts

Hi

Within my application I use block-time. A block-time is a time period in which volunteers work.
For a block of time I want to register the start and end time after which the duration must be calculated.
The date is not important but I do need it to calculate the correct duration. Now I use the following fields:
- StartDate (date field)
- StartTime (time field)
- EndDate (date field)
- EndTime (time field)

With a new record, the current date is stored in the StartDate and EndTime fields.
Calculating the duration goes well when I am in a period between 00: 00-00: 00.

When I specify a time between 2:00 PM and 4:00 PM, the duration is calculated as 2:00 AM.
When I specify a time between 10.00 and 2.00 p.m., the duration is calculated at 8 p.m. but should actually be 4 p.m.

Question:
- which type of field can I use best, DateTime or a separate date and time field
- how can I correctly calculate the duration when the start time <00:00 and the end time> 00:00

Now i use this code to calculate duration:

iAmountMinutes  := MinutesBetween(TypeBlockTime.Fieldbyname('EndTime').value,TypeBlockTime.Fieldbyname('StartTime').Value);
TypeBlockTime.FieldByName('showTotalHours').value :=Format('%2.2d:%2.2d',[iAmountMinutes div 60,iAmountMinutes mod 60]);

Regards Peter

Link to comment
Share on other sites

5 hours ago, mierlp said:

When I specify a time between 2:00 PM and 4:00 PM, the duration is calculated as 2:00 AM.
When I specify a time between 10.00 and 2.00 p.m., the duration is calculated at 8 p.m. but should actually be 4 p.m.

Hi,

Please explain, do you have different formats?

Link to comment
Share on other sites

In my opinion you should use one var (TDateTime) as StartDateTime and one var for EndDateTime.  You should first put the fields StartDate and Starttime to a StartDateTime var and the fields EndDate and EndTime to EndDateTIme (Using EncodeDateTIme function or some other..). Then you can use all the functions from Delphi to calculate duration (MinutesBetween, HoursBetween, DayBetween etc.) the correct way.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...