Date :: Calc - 格式化日期和月份 [英] Date::Calc - format the day and month

查看:327
本文介绍了Date :: Calc - 格式化日期和月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我想在这里做的是如果日期或月份是一个数字,在
的前面加一个零。现在它打印出的日期为201188,我正在寻找
20110808。

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; 


推荐答案

如果您乐意使用 Date :: Calc ,为什么不使用 DateTime

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

这篇关于Date :: Calc - 格式化日期和月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆