Results 1 to 5 of 5

Thread: Perl Question

  1. #1
    Join Date
    Sep 2003
    Location
    Memphis
    Posts
    62

    Unanswered: Perl Question

    Anyone know how I can get yesterday's date in the format of mmddyyyy? It's easy for me in the M$ world, but I haven't ever done ANY perl before.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use:
    Code:
    #  ptp  20040421  Show yesterdays date in mmddyy format
    
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
    
    localtime(time - 86400);
    
    printf("%02d%02d%04d", 1 + $mon, $mday, 1900 + $year);
    -PatP

  3. #3
    Join Date
    Sep 2003
    Location
    Memphis
    Posts
    62
    Originally posted by Pat Phelan
    I'd use:
    Code:
    #  ptp  20040421  Show yesterdays date in mmddyy format
    
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
    
    localtime(time - 86400);
    
    printf("%02d%02d%04d", 1 + $mon, $mday, 1900 + $year);
    -PatP
    heh, thanks, Pat!!! I was needing the time-86400. I'm used to being able to just use some kind of M$ 102,101 date format. I appreciate the help.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I just noticed that my code got butchered when I pasted it... That would be a bad thing!

    -PatP

  5. #5
    Join Date
    Apr 2004
    Posts
    16
    A more flexible way is to look at the strftime function in the POSIX module. With this, you do not need to bother with things like adding 1 to the month (I've forgotten to do this many times using sprintf methods).

    Code:
    use POSIX 'strftime';
    $sid = 60 * 60 * 24;
    print strftime "%m%d%Y", localtime(time - $sid);
    http://www.perldoc.com/perl5.8.0/lib/POSIX.html
    http://www.opengroup.org/onlinepubs/.../strftime.html
    perl -le 'print reverse reverse "just another perl hacker"'
    wush.net subversion hosting - remote hosted revision control with easy admin, ssl security & backups

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •