Is There a Time Difference Calculator That Works with Delphi?

  • Thread starter Thread starter irony of truth
  • Start date Start date
  • Tags Tags
    Time
Click For Summary
SUMMARY

The forum discussion centers on creating a time difference calculator using Delphi. A user shared a Delphi function, SubtractDateTime, which computes the time difference between two TDateTime values and returns the result in seconds. The function utilizes constants for various time units, such as seconds, minutes, and years, and includes error handling for potential range check errors. Additionally, the user offered to provide a Visual Basic (VB) version of the code for broader accessibility.

PREREQUISITES
  • Familiarity with Delphi programming language
  • Understanding of TDateTime data type in Delphi
  • Basic knowledge of time calculations and conversions
  • Experience with error handling in programming
NEXT STEPS
  • Explore Delphi's DecodeDate and DecodeTime functions for date manipulation
  • Learn about error handling techniques in Delphi programming
  • Research how to convert Delphi code to Visual Basic (VB) for cross-platform compatibility
  • Investigate additional libraries or frameworks for date and time calculations in Delphi
USEFUL FOR

Developers working with Delphi who need to implement date and time calculations, as well as those interested in converting Delphi code to other programming languages like Visual Basic.

irony of truth
Messages
89
Reaction score
0
Is there a site which has a "program" that allows me to enter two dates and then the program will compute the length of time between these two dates?
 
Computer science news on Phys.org
why not sit down and figure that out yourself? heck, you could write a perl script to do that/
 
I wrote a function in Delphi that does that. I am not sure if you have Delphi or not. If you really want I can write a VB code and it should be easily transated to VB Script to be used in Web Browsers. By the way, it returns the result in seconds. Here you go. (may get range check error if Date1 > Date2 ^^)

Function SubtractDateTime(Date1, Date2: TDateTime): Extended;
Const
{-Time-}
Millisecond = 0.001;
Second = 1; Seconds = 1;
Minute = 60; Minutes = 60;
Hour = 3600; Hours = 3600;
Day = 86400; Days = 86400;
Week = 604800; Weeks = 604800;
Month = 2629743.83; Months = 2629743.83;
year = 31556926; years = 31556926;
Decade = 315569260; Decades = 315569260;
Century = 3.1556926 * 10e09;
Millenium = 3.1556926 * 10e10;
{-/Time-}
var
Hour1, Min1, Sec1, MSec1 : Word;
Y1, M1, D1: Word;
Hour2, Min2, Sec2, MSec2 : Word;
Y2, M2, D2: Word;
RHour, RMin, RSec, RMSec, RY, RM, RD: Word;
Begin
Try
Result := 0;
DecodeDate(Date1, Y1, M1, D1);
DecodeDate(Date2, Y2, M2, D2);
DecodeTime(Date1, Hour1, Min1, Sec1, MSec1);
DecodeTime(Date2, Hour2, Min2, Sec2, MSec2);
RHour := Hour1 - Hour2;
RMin := Min1 - Min2;
RSec := Sec1 - Sec2;
RMSec := MSec1 - MSec2;
RY := Y1 - Y2;
RM := M1 - M2;
RD := D1 - D2;

Result := Result + RHour * Hour;
Result := Result + RMin * Minute;
Result := Result + RSec * Seconds;
Result := Result + RMSec * MilliSecond;
Result := Result + RY * Year;
Result := Result + RM * Month;
Result := Result + RD * Day;
Except Result := 0;//wtf ^^
End;
End;
 
Last edited:

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
21
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K