Solved

Date calculation

Posted on 1998-06-19
7
155 Views
Last Modified: 2010-04-04
I have two dates in YYYY/MM/DD HH:MM:SS format.  I need to be able to find the number of seconds between the 2 dates.  For Instance I need the number of Seconds between
1997/12/20 14:23:50 and 1998/2/19 10:19:23.

Thanks
0
Comment
Question by:chaas
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:chaas
Comment Utility
None
0
 
LVL 5

Expert Comment

by:inter
Comment Utility
Hi
Delphi 1 or Delphi 2+?
Regards, Igor
0
 
LVL 5

Accepted Solution

by:
inter earned 200 total points
Comment Utility
Any way,

If you represent your dates as
  12/20/1997 14:23:50
    2/19/1998 10:19:23
and call the following function

function DateToSeconds(StartDate, EndDate : string):integer;
var
  FinalDate, StDate : TDateTime;
  Diff : TDateTime;
begin
  Result := -1;
  try
    FinalDate := StrToDateTime(EndDate);
    StDate := StrToDateTime(StartDate);
    Diff := FinalDate - StDate;
    if Diff < 0 then
      ShowMessage('Final date should be > than Start date')
    else
      Result := Round(Diff*24*60*60); //days to seconds
  except
    ShowMessage('Invalid date string!');
  end;
end;
//************************
AS FOLLOWS:
   nofseconds := DateToSeconds( '12/20/1997 14:23:50', '2/19/1998 10:19:23');

It will work, (for Delphi 2.0+ for delphi we should first convert TDateTime to Delphi 2+ format)
Regards, Igor
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:vladika
Comment Utility
Try

function Convert(S: string): TDateTime;
var OldShortDateFormat: string;
    OldDateSeparator, OldTimeSeparator: Char;
begin
  OldShortDateFormat := ShortDateFormat;
  OldDateSeparator := DateSeparator;
  OldTimeSeparator := TimeSeparator;
  try
    ShortDateFormat := 'YYYY/MM/DD';
    DateSeparator := '/';
    TimeSeparator := ':';
    Result := StrToDateTime(S);
  finally
    ShortDateFormat := OldShortDateFormat;
    DateSeparator := OldDateSeparator;
    TimeSeparator := OldTimeSeparator;
  end;
end;

function NumberOfSec(S1, S2: string): Integer;
begin
  Result := Round((Convert(S2)-Convert(S1))*SecsPerDay);
end;

//How to use:

  Secs := NumberOfSec('1998/06/19 06:25:00', '1998/06/19 06:25:10')

// Secs = 10

0
 
LVL 3

Expert Comment

by:vladika
Comment Utility
Try

function Convert(S: string): TDateTime;
var OldShortDateFormat: string;
    OldDateSeparator, OldTimeSeparator: Char;
begin
  OldShortDateFormat := ShortDateFormat;
  OldDateSeparator := DateSeparator;
  OldTimeSeparator := TimeSeparator;
  try
    ShortDateFormat := 'YYYY/MM/DD';
    DateSeparator := '/';
    TimeSeparator := ':';
    Result := StrToDateTime(S);
  finally
    ShortDateFormat := OldShortDateFormat;
    DateSeparator := OldDateSeparator;
    TimeSeparator := OldTimeSeparator;
  end;
end;

function NumberOfSec(S1, S2: string): Integer;
begin
  Result := Round((Convert(S2)-Convert(S1))*SecsPerDay);
end;

//How to use:

  Secs := NumberOfSec('1998/06/19 06:25:00', '1998/06/19 06:25:10')

// Secs = 10

0
 

Author Comment

by:chaas
Comment Utility
Muchos Gracias
0
 
LVL 5

Expert Comment

by:inter
Comment Utility
vladika I am sorry,
It seems that I've block your way. Tell me what can I do for you?
Regards, Igor
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now