• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

Comparing Dates

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
ST3VO
Asked:
ST3VO
  • 15
  • 9
1 Solution
 
MerijnBSr. Software EngineerCommented:
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
 
rfwoolfCommented:
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
 
ST3VOAuthor Commented:
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
ST3VOAuthor Commented:
Opps...my mistake:

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

not

Idhttp1.Head(IntToStr(i)); :o/  hehehe :o)
0
 
ST3VOAuthor Commented:
Now working but not looping.... :o/
0
 
ST3VOAuthor Commented:
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
 
ST3VOAuthor Commented:
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
 
ST3VOAuthor Commented:
Anyone please???
0
 
MerijnBSr. Software EngineerCommented:
if you debug this, what happens if you hit a 404?
0
 
ST3VOAuthor Commented:
Yes, I just need to ignore the 404 message and continue the loop!

0
 
MerijnBSr. Software EngineerCommented:
please read my question:

If you debug this code, what happens if you encounter a 404?
0
 
ST3VOAuthor Commented:
Sorry but how do I do that at runtime? :o(

0
 
MerijnBSr. Software EngineerCommented:
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
 
ST3VOAuthor Commented:
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
 
MerijnBSr. Software EngineerCommented:
what happens if you remove the continue from the except?
0
 
ST3VOAuthor Commented:
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
 
ST3VOAuthor Commented:
Still hungs!!!

It's like it's ignoring the except part :o/
0
 
MerijnBSr. Software EngineerCommented:
does it hang, or does it stop looping?
0
 
ST3VOAuthor Commented:
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
 
MerijnBSr. Software EngineerCommented:
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
 
MerijnBSr. Software EngineerCommented:
oops, forgot the remove the continue; from the except part, take that out please


try
 ...
except
end;
0
 
ST3VOAuthor Commented:
Still hungs!

Used to hung url 40 ....now it hungs on 97....but same error :o/
0
 
MerijnBSr. Software EngineerCommented:
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
 
ST3VOAuthor Commented:
No probs!!!

Thanks!!! I'll keep on trying :o)
0
 
ST3VOAuthor Commented:
Thanks....I've opened another more specific question!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 15
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now