Solved

Date calculation

Posted on 1998-06-19
7
158 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Firemonkey Need Sample for Online Shopping Example. 2 140
Strange behavior when a form is closed 6 57
Delphi 2 59
How to debug For loops? 3 49
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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