mierlp Posted January 27, 2020 Share Posted January 27, 2020 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 More sharing options...
Sherzod Posted January 28, 2020 Share Posted January 28, 2020 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 More sharing options...
Jean-Marc Kiener Posted January 28, 2020 Share Posted January 28, 2020 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 More sharing options...
Jean-Marc Kiener Posted January 28, 2020 Share Posted January 28, 2020 If you works with different timezones then you should read https://www.delphipraxis.net/191522-utc-datetime-string-tdatetime.html Link to comment Share on other sites More sharing options...
mierlp Posted January 28, 2020 Author Share Posted January 28, 2020 @sherzod No different formats. At this point we do it all. @jean-marc Thanks..i will try. There are no different time zones Link to comment Share on other sites More sharing options...
Sherzod Posted January 28, 2020 Share Posted January 28, 2020 1 hour ago, mierlp said: No different formats. At this point we do it all. Can you make a simple testcase for reproduce?! Link to comment Share on other sites More sharing options...
mierlp Posted January 28, 2020 Author Share Posted January 28, 2020 Hi Sherzod Simple test case added testcase - calculate time.zip Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now