How to get current date and time using perl

Last updated on February 17th, 2022 at 07:11 pm

Click to rate this tutorial!
[Total: 2 Average: 3]

This is one of the perfect way of displaying respective server date and time using Perl. We are making use of sprintf and localtime. There are multiple methods to get date but here I am following the simplest one available to get current date using perl.

We are also using strftime and extracting the Hour:Minute:Second as shown below.

#!/usr/bin/perl
use POSIX qw(strftime);
($day, $month, $year) = (localtime)[3,4,5];
$my_date = sprintf("%04d-%02d-%02d\n", $year+1900, $month+1, $day);
print "Todays date is $my_date";
my $get_time=strftime "%H:%M:%S", localtime;
print "Time is $get_time\n";

Sample Output

$ ./today.pl
Todays date is 2022-02-18
Time is 03:09:21


Click to rate this tutorial!
[Total: 2 Average: 3]

Leave a Reply

Your email address will not be published.