Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts

    how to get date in a perl script

    I am trying to let my perl script to create a subfolder using the current date in the format of 'yyyymmdd'.

    Anyone has done that before?

    Thanks.

  • #2
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Code:
    use POSIX qw(strftime);
    
    use strict;
    use warnings;
    
    print strftime "%Y%m%d", localtime;

  • Users who have thanked miller for this post:

    camaross (04-09-2011)

  • #3
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you very much!

    So to make a new directory using it, will be:

    mkdir strftime "%Y%m%d", localtime?
    Last edited by camaross; 04-09-2011 at 05:21 PM.

  • #4
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    You could do that, but you should always error trap your file operations. Therefore, I'd suggest that you put the new dir name in a variable first and use an or die.

    Code:
    my $dir = strftime "%Y%m%d", localtime;
    
    mkdir($dir) or die "Can't mkdir $dir: $!";

  • Users who have thanked miller for this post:

    camaross (04-09-2011)

  • #5
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you very much!

    Quote Originally Posted by miller View Post
    You could do that, but you should always error trap your file operations. Therefore, I'd suggest that you put the new dir name in a variable first and use an or die.

    Code:
    my $dir = strftime "%Y%m%d", localtime;
    
    mkdir($dir) or die "Can't mkdir $dir: $!";


  •  

    Posting Permissions

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