There is an ongoing outage on the primary CPAN mirror. It is possible to work around the issue by using MetaCPAN as a mirror.

PDK::Utils::Date - 日期时间工具类

SYNOPSIS

use PDK::Utils::Date;

my $dateUtil = PDK::Utils::Date->new;

say $dateUtil->getCurrentYearMonth();     # 2025-09
say $dateUtil->getCurrentYearMonthDay();  # 2025-09-09
say $dateUtil->getFormatedDate("yyyy-mm-dd hh:mi:ss");

DESCRIPTION

该模块提供常用的日期与时间处理方法,包括获取当前日期、年月、以及自定义格式化输出。

METHODS

getCurrentYearMonth

my $ym = $dateUtil->getCurrentYearMonth();

获取当前年月,返回字符串格式:

YYYY-MM

示例:

2025-09

getCurrentYearMonthDay

my $ymd = $dateUtil->getCurrentYearMonthDay();

获取当前年月日,返回字符串格式:

YYYY-MM-DD

示例:

2025-09-09

getFormatedDate([$format, $time])

my $date = $dateUtil->getFormatedDate("yyyy-mm-dd hh:mi:ss", time());

根据指定格式与时间戳返回格式化的日期字符串。

支持两种调用方式:

  • (格式, 时间)

  • (时间, 格式) —— 当第一个参数为纯数字时

  • $format - 格式字符串,默认 yyyy-mm-dd hh:mi:ss

  • $time - Unix 时间戳,默认当前时间 (time)

可用格式符:

  • yyyy - 四位年份 (如 2025)

  • mm - 两位月份 (01–12)

  • dd - 两位日期 (01–31)

  • hh - 两位小时 (00–23)

  • mi - 两位分钟 (00–59)

  • ss - 两位秒数 (00–59)

示例:

# 当前时间,默认格式
$dateUtil->getFormatedDate();
# 输出: 2025-09-09 21:35:42

# 指定格式
$dateUtil->getFormatedDate("yyyy年mm月dd日 hh:mi");
# 输出: 2025年09月09日 21:35

# 指定时间戳
$dateUtil->getFormatedDate(1609459200, "yyyy/mm/dd");
# 输出: 2021/01/01

ERROR HANDLING

  • 当格式字符串中未包含任何有效格式符时,会抛出异常。

EXAMPLES

use PDK::Utils::Date;

my $dateUtil = PDK::Utils::Date->new;

say $dateUtil->getCurrentYearMonth();     # 2025-09
say $dateUtil->getCurrentYearMonthDay();  # 2025-09-09
say $dateUtil->getFormatedDate("yyyy-mm-dd hh:mi:ss");

AUTHOR

WENWU YAN <968828@gmail.com>

LICENSE AND COPYRIGHT

This software is licensed under the same terms as Perl itself.