Solved

Comparing Dates

Posted on 2007-11-28
25
398 Views
Last Modified: 2010-04-21
Hi all,

I am getting the Last Modified Stamp on images online using Idhttp1.Head command.

Like this:

Idhttp1.Head('http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV15462190_1b.jpg');
Edit1.Text:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']);

This is returning:
Wed, 29 Aug 2007 11:49:57 GMT

Then I have today's datetime in the same format:
Code: s:=FormatDateTime('ddd, d mmm yyyy hh:mm:ss "GMT"', now);

Which returns:
Wed, 28 Nov 2007 12:01:02 GMT

Up to there all ok!

What I need to know is if the image was modified Today or now.

So, if the image LastModified Stamp is older than today do something....

If the image LastModified Stamp was modified today then do something else...

I don't know if this is right but as I don't want to compare the time etc...Just the dates I have Trimed
the data so it just leaves to date.

Like this:

DataTimeNow:=AnsiLeftStr(DateTimeNow, 16);
ImageStampDate:=AnsiLeftStr(ImageStampDate, 16);

This leave me with: Wed, 28 Nov 2007

Now....

How do I compare the date so I know if the image was modified today or not???

Hope you can help

Thanks

ST3VO
0
Comment
Question by:ST3VO
  • 15
  • 9
25 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20365140
if you only want to check if it wasn't today simply see if the strings match:
 ...
 

 DataTimeNow:=AnsiLeftStr(DateTimeNow, 16);

 ImageStampDate:=AnsiLeftStr(ImageStampDate, 16);
 

 if DateTimeNow = ImageStampDate then

 begin

  // it was today!

 end;

Open in new window

0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20365192
Okay when comparing dates you must get them into date format (can't leave them as strings), so for this you can use StrToDate function:
  function StrToDate(const S: string; var FormatSettings: TFormatSettings): TDateTime; overload;
You can look this up in the delphi help file, but here is an example that might work:

StrToDate(MyString, FormatSettings);

(You just need to look up TFormatSettings to get this right)

Then to compare two dates you can add DateUtils to your unit and use this:
function CompareDate(const A: TDateTime; const B: TDateTime): TValueRelationship;
example:
If CompareDate(DateOne, DateTwo) = 1 then //Then DateOne > DateTwo
...
If CompareDate(DateOne, DateTwo) = -1 then //Then DateOne < DateTwo
0
 

Author Comment

by:ST3VO
ID: 20365318
I'm trying this but get "unknown protocol" error:

Any Ideas???
procedure TForm1.Button3Click(Sender: TObject);
 

var ImageStampDate, DataTimeNow : String;

i: integer;

begin

for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items

    begin

      Idhttp1.Head(IntToStr(i)); //Assign the current selection count

      ImageStampDate:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get Last Modified from image online

      DataTimeNow:=FormatDateTime('ddd, d mmm yyyy hh:mm:ss "GMT"', now); //Get Now FormatDate

      ImageStampDate:=AnsiLeftStr(ImageStampDate, 16); //Only leave the Date

      DataTimeNow:=AnsiLeftStr(DataTimeNow, 16);  //Only leave the Date

      if DataTimeNow <> ImageStampDate then listbox1.Items.Delete(i); // Clear it not new
 

    end;

end;

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20365358
Opps...my mistake:

It's  Idhttp1.Head(Listbox1.Items[i]); //Assign the current selection count

not

Idhttp1.Head(IntToStr(i)); :o/  hehehe :o)
0
 

Author Comment

by:ST3VO
ID: 20365360
Now working but not looping.... :o/
0
 

Author Comment

by:ST3VO
ID: 20365412
Update:

This is now working...although I need to test the results....

procedure TForm1.Button3Click(Sender: TObject);
 

var ImageStampDate, DataTimeNow : String;

i: integer;

begin

for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items

    begin

      //if i > 0 then

       Begin

      Idhttp1.Head(Listbox1.Items[i]); //Assign the current selection count

      ImageStampDate:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get Last Modified from image online

      DataTimeNow:=FormatDateTime('ddd, d mmm yyyy hh:mm:ss "GMT"', now); //Get Now FormatDate

      ImageStampDate:=AnsiLeftStr(ImageStampDate, 16); //Only leave the Date

      DataTimeNow:=AnsiLeftStr(DataTimeNow, 16);  //Only leave the Date

      if DataTimeNow <> ImageStampDate then

        begin

        listbox1.Items.Delete(i); // Clear it not new

        Edit1.Text:=IntToStr(i);

        end;

        Application.ProcessMessages;

       end;

    end;

end;

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20365496
OK.....The loops stops if the url is not found...404 message.

I'm trying

Try

Except

But it's not continuing...

Any ideas?

Here is the code:


procedure TForm1.Button3Click(Sender: TObject);
 

var ImageStampDate, DataTimeNow : String;

i: integer;

begin
 

  for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items

    begin

      //if i > 0 then

      // Begin
 

    try

      Idhttp1.Head(Listbox1.Items[i]); //Assign the current selection count

      ImageStampDate:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get Last Modified from image online

      DataTimeNow:=FormatDateTime('ddd, d mmm yyyy hh:mm:ss "GMT"', now); //Get Now FormatDate

      ImageStampDate:=AnsiLeftStr(ImageStampDate, 16); //Only leave the Date

      DataTimeNow:=AnsiLeftStr(DataTimeNow, 16);  //Only leave the Date

      if DataTimeNow <> ImageStampDate then

        begin

        listbox1.Items.Delete(i); // Clear it not new

        Edit1.Text:=IntToStr(i);

       end;

      Application.ProcessMessages;

    Except

      continue;

    end;
 

end;

end;

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20384315
Anyone please???
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20384329
if you debug this, what happens if you hit a 404?
0
 

Author Comment

by:ST3VO
ID: 20384384
Yes, I just need to ignore the 404 message and continue the loop!

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20384562
please read my question:

If you debug this code, what happens if you encounter a 404?
0
 

Author Comment

by:ST3VO
ID: 20384592
Sorry but how do I do that at runtime? :o(

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:MerijnB
ID: 20384611
place a breakpoint in the code (set your cursor on line 7) and press F5. A red dot will appear in front of the line.

Now run the code, it will stop at the place you've put the breakpoint.

Now by pressing F8, you 'walk' through the lines one by one. See what happens if you come across the 404.
0
 

Author Comment

by:ST3VO
ID: 20384674
It says No Debug Info...

But after a long while I get a 404 exception message

Debugger Exception Notification

Project Project1.exe raised exception class EidHttpProtocolException with message 'HTTP/1.1 404 Not Found'.


Breaks on this line:

Idhttp1.Head(Listbox1.Items[i]); //Assign the current selection count
ImageStampDate:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); <---HERE

But it could be the one above!

Hope this helps!
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20384785
what happens if you remove the continue from the except?
0
 

Author Comment

by:ST3VO
ID: 20384792
I just want to skip that error so the loop continues if the error occures. That's all I need!


I've even tryed:

Try

Except
  break;

But it still hangs :o(
0
 

Author Comment

by:ST3VO
ID: 20384803
Still hungs!!!

It's like it's ignoring the except part :o/
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20384812
does it hang, or does it stop looping?
0
 

Author Comment

by:ST3VO
ID: 20384821
I've taken the try ...except off...as it;s doing nothing...

This is my current code:


procedure TForm1.Button3Click(Sender: TObject);
 

var ImageStampDate, DataTimeNow : String;

i: integer;

begin
 

  for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items
 

    begin

      if i > 0 then

       Begin
 

 

      Idhttp1.Head(Listbox1.Items[i]); //Assign the current selection count

      ImageStampDate:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get Last Modified from image online
 

      DataTimeNow:=FormatDateTime('ddd, d mmm yyyy hh:mm:ss "GMT"', now); //Get Now FormatDate

      ImageStampDate:=AnsiLeftStr(ImageStampDate, 16); //Only leave the Date

      DataTimeNow:=AnsiLeftStr(DataTimeNow, 16);  //Only leave the Date

      if DataTimeNow <> ImageStampDate then

        begin

        listbox1.Items.Delete(i); // Clear it not new

        Edit1.Text:=IntToStr(i);

       end

      end;

      Application.ProcessMessages;
 

    end;

end;

Open in new window

0
 
LVL 19

Accepted Solution

by:
MerijnB earned 500 total points
ID: 20384847
put in the try except, but don't put anything in the except part
procedure TForm1.Button3Click(Sender: TObject);

 

var ImageStampDate, DataTimeNow : String;

i: integer;

begin

 

  for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items

    begin

      //if i > 0 then

      // Begin

 

    try

      Idhttp1.Head(Listbox1.Items[i]); //Assign the current selection count

      ImageStampDate:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get Last Modified from image online

      DataTimeNow:=FormatDateTime('ddd, d mmm yyyy hh:mm:ss "GMT"', now); //Get Now FormatDate

      ImageStampDate:=AnsiLeftStr(ImageStampDate, 16); //Only leave the Date

      DataTimeNow:=AnsiLeftStr(DataTimeNow, 16);  //Only leave the Date

      if DataTimeNow <> ImageStampDate then

        begin

        listbox1.Items.Delete(i); // Clear it not new

        Edit1.Text:=IntToStr(i);

       end;

      Application.ProcessMessages;

    Except

      continue;

    end;

 

end;

end;

Open in new window

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20384855
oops, forgot the remove the continue; from the except part, take that out please


try
 ...
except
end;
0
 

Author Comment

by:ST3VO
ID: 20384878
Still hungs!

Used to hung url 40 ....now it hungs on 97....but same error :o/
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20384900
I could try to reproduce it here, but I'll need the list of img urls (or at least one that goes wrong).
Unfortunately, I don't have time for that now, maybe monday :|
0
 

Author Comment

by:ST3VO
ID: 20384905
No probs!!!

Thanks!!! I'll keep on trying :o)
0
 

Author Closing Comment

by:ST3VO
ID: 31411395
Thanks....I've opened another more specific question!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 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

13 Experts available now in Live!

Get 1:1 Help Now