perl如何将字符串转换为Datetime? [英] perl how to convert a string to Datetime?

查看:292
本文介绍了perl如何将字符串转换为Datetime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将字符串转换为Perl中的日期,但是会收到错误。

I tried to convert a string to date in perl, but get error.

use strict; 
use warnings;  
use DateTime;  
use Date::Manip;

my $date = ParseDate("20111121");
print "today is ".$date->day_of_week."\n"; 

错误

Can't call method "day_of_week" without a package or object reference 

看起来像包导入有问题...

Looks like the package import has problem ...

谢谢

推荐答案

DateTime doesn解析日期。我会为Time :: Piece核心模块提供strptime():

DateTime doesn't parse dates. I'd go for the Time::Piece core module that gives you strptime():

#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t = Time::Piece->strptime("20111121", "%Y%m%d");
print $t->strftime("%w\n");

这篇关于perl如何将字符串转换为Datetime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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