[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Dates

Posted on 2000-05-02
11
Medium Priority
?
156 Views
Last Modified: 2010-04-04
I'm creating a program which only can do a certain thing once per day.
This means that I have to be able to find out whether this has been done "today".  I'm not sure how to do this, but I thought of something like this:

1. Get current date or something similar.
2. Save it to database or whatever.
3. Next time this is attempted, then read from database, get current date again and figure out if one day has passed.

How can I compare two dates to see if there is atleast one day difference?
Atleast see if the day of month is the same in both dates or not.

This is probably not a complicated task, but I don't know where to begin ...

Thanks in advance!
0
Comment
Question by:hagur
[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
11 Comments
 
LVL 2

Expert Comment

by:rene100
ID: 2771070
Hi

To get the current date, use this:

var
Today: TDateTime;
begin
Today:=Date;
end;

If you want to know wich day was yesterday,simply use Today-1, tomorrow is Today+1 and so on....

regards
rene
0
 

Expert Comment

by:mr76
ID: 2771101
To Compare the date you stored when you did the action you could use code like this:

if table1DATEFIELD.asdatetime <> Date then
begin
  DoYourCodeHere;
end;

mr76
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2771287
You can store a date in a database like this:

Table1.FieldByName('Date').AsDateTime := Date;

or

Table1.FieldByName('Date').AsDateTime := Now;

to get the date and time.

To read it back:

var thedate: TDateTime;

thedate := Table1.FieldByName('date').AsDateTime;


Regards,

Epsylon.
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 12

Accepted Solution

by:
rwilson032697 earned 300 total points
ID: 2771478
This seems to be the crux of your Q:

"How can I compare two dates to see if there is atleast one day difference? "

Function DatesAreDifferentDays(Date1, Date2 : TDateTime) : Boolean;
begin
  result := trunc(Date1) <> trunc(Date2);
end;

Epsylon has handily shown you how to store a date in a table, though you could also store it in an INI file or the registry...

Cheers,

Raymond.

 
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2772090
hmm,
just all said,
only to remark that you must store the date
of last run to keep a date-comparebase

then do as follows
read the last-run-date from whatever (file, table, registry, ini)
compare the date with today
if different
  run your job
  if job-run ok
    store todays-date as lastrundate
  else
    some error log

(just a sceleton)

meikl

0
 

Expert Comment

by:mrissmann
ID: 2772132
I agree with rwilson, the registry is probably the best and fastest way to do this.

Are these the answers to your question?

Is it once per day or once per 24 hour period?

Mark
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2772364
Hi all.
Hi hagur.

The working sample is bellow. This is a compilation of above ideas.
------------
program Project1;

uses
  windows,
  registry,
  sysutils,
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

function GetLocalRegistry : TRegistry;
var S : string;
begin
   S:=ExtractFileName(Application.ExeName);
   SetLength(S,length(S)-4); // delete .exe extension
   result:=TRegistry.Create;
   result.RootKey:=HKEY_LOCAL_MACHINE;
   result.OpenKey('software\'+S,true);
end;

function NeedToRun : boolean;
var R : TRegistry;
    D : TDateTime;
begin
   R:=GetLocalRegistry;
   try
     D:=R.ReadDate('last_run');
   except
     D:=0;
   end;
   R.Free;
   result:=Int(D) <> Int(Date);
end;

procedure MarkToday;
var R : TRegistry;
begin
   R:=GetLocalRegistry;
   R.WriteDate('last_run',Date);
   R.Free;
end;

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  if NeedToRun then
  begin
    Application.Run;
    MarkToday;
  end;
end.
-----
Igor.
0
 
LVL 1

Expert Comment

by:Asw
ID: 2773101
Listening.
0
 
LVL 3

Author Comment

by:hagur
ID: 2773582
Thank you all for your comments!

I will be able to do this now.  But I'm not sure which one should get the points.

Epsylon gave a good example of how to read/write dates to a database,

Rwilson gave a good example of comparing dates.

kretzschmar, this is exactly like I had planned doing this.  It was nice though to get a skeleton of the flow ...

And finally Igor, Nice to see the code in complete state.


What do you think is fair?
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2775126
Its up to you. If you want to split points you will need to post separate Q's with the appropriate points for each expert. Otherwise pick the answer the suits you best and reward it.

Cheers,

Raymond.
0
 
LVL 3

Author Comment

by:hagur
ID: 2778620
I think I'll have to accept Raymonds answer, he answered the part of my question I really needed.  I knew how to save and load from database etc ...

Thanks anyway for everything!

- Hagur
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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