How to print date and time in Perl using strftime

Home > Search > How-to

Use the POSIX strftime module.

use POSIX 'strftime';

$date = strftime "%m %d, %Y", localtime;
$time = strftime "%r", localtime;

print $date at $time;


This will produce a result in this format.

01 16, 2018 at 06:05:39 AM


Following are commonly used options.

Option Description Example
%Y Year 2018
%y Year 18
%m Month 01
%b Month Jan
%B Month January
%d Day 14
%H Hour 07
%M Minute 56
%S Second 23


Future / Past date

The time+ and time- options can be used to adjust the date to a future or past date. 86400 (minutes) equals one day.

$yesterday = strftime "%m %d, %Y", localtime(time-86400);
$tomorrow  = strftime "%m %d, %Y", localtime(time+86400);


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.