Solved

Date calculation

Posted on 1998-06-19
7
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:chaas
ID: 1354135
None
0
 
LVL 5

Expert Comment

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

Accepted Solution

by:
inter earned 200 total points
ID: 1354137
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:vladika
ID: 1354138
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
ID: 1354139
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
ID: 1354140
Muchos Gracias
0
 
LVL 5

Expert Comment

by:inter
ID: 1354141
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 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