Reputation: 2635
All I am trying to do here is if the day or month is a single digit, to add a zero in the front of it. Right now it prints out the date as 201188, and I am looking for 20110808.
#!/usr/bin/perl
use Date::Calc qw(Add_Delta_Days);
my (undef, undef, undef, $day, $month, $year) = localtime();
$year +=1900;
$month +=1;
($year, $month, $day ) = Add_Delta_Days($year, $month, $day, -3)
if ($month =~ /\d{1}/){
s/$month/0$month/
}
if ($day =~/\d{1}/){
s/$day/0$day/
}
print $year,$month,$day;
Upvotes: 5
Views: 7321
Reputation: 67918
Use printf
instead:
printf "%d-%02d-%02d", $year, $month, $day;
Gives output such as:
C:\perl>perl -we "printf qq(%d-%02d-%02d), 2011,5,4"
2011-05-04
C:\perl>perl -we "printf qq(%d-%02d-%02d), 2011,5,12"
2011-05-12
C:\perl>perl -we "printf qq(%d-%02d-%02d), 2011,22,12"
2011-22-12
Upvotes: 4
Reputation: 9188
If you're happy to use Date::Calc
, why not use DateTime
?
use DateTime;
my $date = DateTime->now;
$date->subtract(days => 3);
print $date->ymd;
In fact you can reduce that to:
print DateTime->now->subtract(days => 3)->ymd
Upvotes: 5
Reputation: 1818
if ($month < 10)
{
$month="0$month";
}
if ($day < 10)
{
$day="0$day";
}
Upvotes: 2