Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date calculation

Posted on 1998-06-19
7
Medium Priority
?
165 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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