Hi,

Date::Format from CPAN can aid you in formatting dates. As for Modification times, you want to use the stat function.

To print the modification date of a file in dd/mm/yy format you could use:

use Date::Format; $file = '...filepath...'; $modTimeInSec = (stat $file)[9]; print time2str("%d/%m/%y", $modTimeInSec);

For more info on formatting dates, check the documentation for Date::Format.

Your second question is a little more difficult. I will give you some places to look, but how you want to do it will ultimately depend on how exactly you want this to work.

How does the user enter the date range? Is it an exact date-time, or the current day, or all files in a month? etc. What ever you decide, you may want to use Date::Parse to help you parse dates and times.

Once you have a start time and end time in seconds since epoch, then you need to search through your files. For example, to get and array of files in the current directory that were modified between $startTime and $endTime, you might use the following:

@files = grep { my $mod = (stat $_)[9]; $startTime <= $mod && $mod <= $endTime } <*.*>;

This is a quick example and isn't perfect. The glob operator (<*.*>) specifies a glob pattern to match. If you want to search through directories recursively, check out File::Find.

Sorry, I can't be more of a help with your second problem, but it really depends on a better explanation of what you are trying to do. I hope my tips help.

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

In reply to Re: perl dates by TedYoung
in thread perl dates by rjsaulakh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.