【问题标题】:Copying the date in datetimeformat to a variable in perl将 datetimeformat 中的日期复制到 perl 中的变量
【发布时间】:2013-05-30 18:35:19
【问题描述】:

如何将当前日期以日期时间格式复制到 perl 中的变量中?

假设当前日期为 2013-05-30,则将值复制到变量中

$time='2013-05-30 15:10:23' 

如何在 perl 中做到这一点?

【问题讨论】:

    标签: perl datetime


    【解决方案1】:

    使用localtimesprintf

    my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
    my $time = sprintf '%d-%02d-%02d %02d:%02d:%02d', 1900+$year, 1+$mon, $mday, $hour, $min, $sec;
    

    或者,使用Time::Piece(自 5.9.5 以来的核心):

    use Time::Piece;
    my $time = localtime->strftime('%Y-%m-%d %H:%M:%S');
    

    【讨论】:

    • 是的……这行得通……但是有什么更短的东西可以包含在代码中
    • @Vishu:见第二个选项。
    • @Vishu my $date = localtime 会给你一个时间戳,虽然不是你想要的格式。
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多