Days passed since..

Posted on 2004-04-27
Last Modified: 2010-04-05
I need a function to give me how many days have passed
since a given data. It has to keep in mind about different
data formats.

Question by:bryan7
LVL 27

Accepted Solution

kretzschmar earned 500 total points
ID: 10926022
>It has to keep in mind about different data formats.

just transform it into tdatetime

function DaysBetween(d1,d2 : TDateTime) : Integer;
  result := Abs(trunc(d1-d2));

meikl ;-)

LVL 12

Expert Comment

ID: 10926065

  yep, kretzschmar is right, but let me add something... you don't need the Abs function because ...

  TDateTime is real number. The integer part represent the days after 12/30/1899. So trunc(d1-d2) will give you the number of days... This can me negative value ...
LVL 27

Expert Comment

ID: 10926088
>This can me negative value ...

i added abs, to avoid negative values :-))
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10926103
guys the DaysBetween function already exists in DateUtils.pas that gives the integer value of the difference between 2 dates...

also it contain DaySpan function that gets the difference included the frac part of entire day...

from Delphi help:

Call DaysBetween to obtain the difference, in days, between two TDateTime values.

DaysBetween counts only whole days. Thus, DaysBetween reports the difference between Dec 31, 1999 11:59 PM and Jan 1, 2000 11:58 PM as 0 because the difference is one minute short of an entire day.

Call DaySpan to obtain the difference, in days, between two TDateTime values. Unlike the DaysBetween function, which only counts whole days, DaySpan reports incomplete days as a fraction of an entire day.
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

LVL 27

Expert Comment

ID: 10926109
i know, f68 :-))
LVL 27

Expert Comment

ID: 10926111
just to say, that DateUtils.pas is not on all delphi versions available
LVL 27

Expert Comment

ID: 10926129
function DaySpan(d1,d2 : TDateTime) : Double;
  result := Abs(d1-d2);

meikl ;-)

Author Comment

ID: 10926141
Ahh thanks, that was easier than I thought eheh.
EncodeTime will do the rest. Btw my delphi5 enterprise
doesn't include that unit :(

grats kretzs on the first working answer

Thanks all for the help :)

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

16 Experts available now in Live!

Get 1:1 Help Now