Dave's Brain

Browse - Programming Tips - How can I get the current year in perl?

Date: 2016feb28
Language: perl
Keywords: getYear, now, today

Q.  How can I get the current year in perl?

A.

sub getCurrentYear() { return (localtime)[5] + 1900; }
Or slightly less compactly:
sub getCurrentYear() { my(@tm) = localtime(time()); return $tm[5] + 1900; }
Example use:
my $year = getCurrentYear(); print "year=$year\n";

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.