Date Manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeferns79
    New Member
    • Sep 2008
    • 37

    Date Manipulation

    Is there a way of getting the previous date when we enter a specific date?

    e.g if we enter ... 20090804, it should return 20090803, or even if we enter a future date...20090910 , it should return 20090909.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Originally posted by joeferns79
    Is there a way of getting the previous date when we enter a specific date?

    e.g if we enter ... 20090804, it should return 20090803, or even if we enter a future date...20090910 , it should return 20090909.
    You can use one of the Date modules listed on CPAN or you can write your own code to split the date into tokens (year, month, day) and feed them to Time::Local ( that comes with perl) to convert them into epoch seconds and subtract 24 hours of seconds from the epoch time.
    Proof of concept:

    Code:
    use Time::Local;
    
    my $date = '20090910';
    my ($y,$m,$d) = unpack("A4A2A2",$date);
    my $epoch = timelocal(0,0,0, $d, $m-1, $y) - (60*60*24);
    print scalar localtime($epoch);
    assumes the date is always in YYYYMMDD format, if not you will have to change unpack() to a regexp and do some checking. You can use POSIX to reformat $epoch back into YYYYMMDD. I leave that part up to you. Or as mentioned, look into a Date module.

    Comment

    • joeferns79
      New Member
      • Sep 2008
      • 37

      #3
      Thanks a lot, that worked.

      Comment

      Working...