Solved

Date calculation

Posted on 1998-06-19
7
159 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

809 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