【发布时间】:2012-08-17 20:22:19
【问题描述】:
我的代码:
print DateTime->now;
回复:
2012-08-17T20:16:37
为什么会有 T?有没有我忘记的选项?
【问题讨论】:
我的代码:
print DateTime->now;
回复:
2012-08-17T20:16:37
为什么会有 T?有没有我忘记的选项?
【问题讨论】:
T 只是一种标准 (ISO 8601) 方式来分隔时间。要使用其他格式,请考虑使用strftime 或format_cldr。
例如,要使用空格,请使用DateTime->now->format_cldr("YYYY-MM-dd hh:mm:ss")。
【讨论】:
字符串化DateTime 对象使用ISO 8601 格式,除非您在构造函数中指定了格式化程序。请参阅文档中的Formatters and Stringification。
iso8601 方法是:
sub iso8601 { join 'T', $_[0]->ymd('-'), $_[0]->hms(':') }
【讨论】:
这是DateTime 产生的默认输出格式,ISO-8601。如果你想要别的东西,你需要使用strftime 或format_cldr 方法或DateTime::Format::* 模块之一来输出不同的格式,例如:
print DateTime->now->format_cldr("YYYY-MM-dd hh:mm:ss");
【讨论】:
这是日期和时间的 iso 标准,请参阅 http://en.wikipedia.org/wiki/ISO_8601
参见例如How do you read the system time and date in Perl? 讨论以可格式化的方式读取日期/时间。
【讨论】: