11

how do i get the current year in 4 digit this is what i have tried

 #!/usr/local/bin/perl

 @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
 @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
 $year = $year+1900;
 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
 print "DBR_ $year\\$months[$mon]\\Failures_input\\Failures$mday$months[$mon].csv \n";

This prints DBR_ 114\Apr\Failures_input\Failures27Apr.csv

How do I get 2014?

I am using version 5.8.8 build 820.

3
  • 2
    Always include use strict; and use warnings at the top of EVERY perl script. This would've helped you catch your coding error.
    – Miller
    Apr 27, 2014 at 19:14
  • 1
    You have a couple of good answers. But I wonder why you thought that posting here would be a faster way to get answer than just reading the documentation :-)
    – Dave Cross
    Apr 28, 2014 at 10:10
  • i did not include 'use strict and use warnings' as it will not work. $year unrecognized , $month unrecognizedet etc and version 5.8.8 does not have the module time:peice Apr 28, 2014 at 13:28

6 Answers 6

17
use Time::Piece;

my $t = Time::Piece->new();
print $t->year;
13

Move the line:

$year = $year+1900;

To after that call to localtime() and to become:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year = $year+1900;
2
  • 11
    FYI - you can just say my $year = 1900 + (localtime)[5]; Feb 14, 2016 at 20:08
  • @EricFossum Yes, that's also possible if you doesn't need other return values of localtime.
    – Lee Duhem
    Feb 15, 2016 at 1:47
6

The best way is to use the core library Time::Piece. It overrides localtime so that the result in scalar context is a Time::Piece object, you can use the many methods that the module supplies on it. (localtime in list context, as you have used it in your own code, continues to provide the same nine-element list.)

The strftime method allows you to format a date/time as you wish.

This very brief program produces the file path that I think you want (I doubt if there should be a space after DBR_?) Note that there is no need to double up backslashes inside a single-quoted string unless it is the last character of the string.

use strict
use warnings;

use Time::Piece;

my $path = localtime->strftime('DBR_%Y\%b\Failures_input\Failures%m%d.csv');

print $path;

output

DBR_2014\Apr\Failures_input\Failures27Apr.csv
5

One option to get the 4 digit year:

#!/usr/bin/perl

use POSIX qw(strftime);

$year = strftime "%Y", localtime;

printf("year %02d", $year);
1

You can also use

my ($y,$m,$d) = Date::Calc::Today();

$y variable will contain 2019
$m variable will contain 8
$d variable will contain 9

at the time of writing this answer ( 9th August 2019 )

0

The simplest way, I find, to get the year is:

my $this_year = (localtime)[5] + 1900;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.