Format Date and Time in Perl


You can use localtime() function in Perl to get a list of 9-elements and later you can use the printf() function to format date and time based on your requirements as follows −

Example

 Live Demo

#!/usr/local/bin/perl
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
printf("Time Format - HH:MM:SS\n");
printf("%02d:%02d:%02d", $hour, $min, $sec);

Output

When the above code is executed, it produces the following result −

Time Format - HH:MM:SS
06:58:52

Updated on: 29-Nov-2019

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements