Perl 中的今天日期,格式为 MM/DD/YYYY [英] Today's Date in Perl in MM/DD/YYYY format

查看:17
本文介绍了Perl 中的今天日期,格式为 MM/DD/YYYY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Perl 程序,并坚持(我认为是)一个微不足道的问题.我只需要构建一个格式为 '06/13/2012' 的字符串(总是 10 个字符,因此小于 10 的数字为 0).

I'm working on a Perl program at work and stuck on (what I think is) a trivial problem. I simply need to build a string in the format '06/13/2012' (always 10 characters, so 0's for numbers less than 10).

这是我目前所拥有的:

use Time::localtime;
$tm=localtime;
my ($day,$month,$year)=($tm->mday,$tm->month,$tm->year);

推荐答案

只用一个POSIX

You can do it fast, only using one POSIX function. If you have bunch of tasks with dates, see the module DateTime.

use POSIX qw(strftime);

my $date = strftime "%m/%d/%Y", localtime;
print $date;

这篇关于Perl 中的今天日期,格式为 MM/DD/YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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