Perl

Working With Date and Time in PERL

“PERL has many built-in functions to handle the date and time-related data. Using the DateTime module is the most efficient way to handle the date and time-related data. The date and time value can be represented in various ways by using the DateTime class. The ways of printing date and time values with or without using the DateTime module have been shown in this tutorial.”

Perl POSIX Specifiers

The formatted date and time values can be printed by using the strftime() function of PERL by using different types of specifiers preceded with the (%) sign. Two types of specifiers are used in PERL. These are local time and GMT zone. The purposes of using different types of specifiers have been described in the next part of this tutorial.

Local Specifiers

The commonly used local specifiers are mentioned below.

Specifier Purpose
%A It is used to print the full weekday name. Ex- Sunday.
%a It is used to print the weekday name in short form. Ex- Sun.
%B It is used to print the full month name. Ex- January.
%b, %h It is used to print the month name in a short name. Ex- Jan
%x It is used to print the date value in the short form. Ex- 17/06/22
%X It is used to print the time value in 24-hour format. Ex- 16:45:30
%r It is used to print the time value in 12-hour format with AM/PM. Ex- 6:45:30 PM
%c It is used to print the date and time value. Ex- Fri June 17 5:45:10 2022
%Z It is used to print the time zone.

GMT Specifiers

The commonly used GMT specifiers are mentioned below.

Specifier Purpose
%d It is used to print the day of the month with zero padding. (01-31)
%D It is used to print the date value in the short form. Ex- 17/06/22
%e It is used to print the day of the month without zero padding. (1-31)
%F It is used to print the date value with the four-digit year in the short form. Ex- 2022/06/17
%H It is used to print the hour value in 24-hour format. Ex- 15
%I It is used to print the hour value in 12-hour format. Ex- 08
%J It is used to print the day of the year. (000-365)
%m It is used to print the month in decimal format. (01-12)
%M It is used to print the minute value. (00-59)
%p It is used to display AM/PM.
%S It is used to print the second value. (00-59)
%u It is used to print the weekday value as a number starting from Monday. (1-7)
%w It is used to print the weekday value as a number starting from Sunday. (0-6)
%y It is used to print the last two digits of the year.
%Y It is used to print the full year value.

Example-1: Use of localtime() Function

Create a PERL file with the following code that shows the uses of the localtime() function to print the current date and time of the system.

#Read the current date and time using localtime()
$datetime = localtime();  
#Print the current date and time value
print "Current date and time is : $datetime\n";

Output:
The following output will appear after executing the above code.

Example-2: Compare the Output of gmtime() and localtime() Functions

The output of the gmtime() function is similar to the localtime() function. The gmtime() function returns the value depends on the standard Greenwich time zone.

#Read the current date and time using gmtime()
$gmt = gmtime();  
print "The current date and time value using gmtime() function:\n", "$gmt\n";  
#Read the current date and time using localtime()
$local = localtime();  
print "The current date and time value using localtime() function:\n", "$local\n";

Output:
The following output will appear after executing the above code. The following output shows that the output of both gmtime() and localtime() functions are the same.

Example-3: Print the Current Date and Time Function Using the DateTime Module

The DateTime module of the PERL is not installed on the system by default. Run the following commands to install the DateTime module of PERL.

$ sudo apt update
$ sudo apt install libdatetime-perl

Create a PERL file with the following code that shows the uses of the DateTime module to print the current date and time of the system.

#Add the DateTime module
use DateTime;  
#Read the current date and time value
my $datetime = DateTime->now;  
#Print the current date and time value
print "The current date and time value is $datetime\n";

Output:
The following output will appear after executing the above code.

Example-4: Print the Specific Date and Time Function Using DateTime Module

Create a PERL file with the following code that shows the uses of DateTime module to print the specific date and time.

#Add the DateTime module
use DateTime;  
#Set the specific date and time value
$datetime = DateTime->new(
    day => 15,
    month => 6,
    year => 2022,
    hour => 8,
    minute => 35,
    second => 30);  
#Print the specific date and time value
print "The date and time value is $datetime\n";

Output:
The following output will appear after executing the above code.

Example-5: Print the Formatted Date and Time

Create a PERL file with the following code that shows the uses of DateTime module to set the particular date and time value and print that date and time values with formatting.

#Add the DateTime module
use DateTime;  
#Set the specific date and time value
$datetime = DateTime->new(
    day => 20,
    month => 6,
    year => 2022,
    hour => 12,
    minute => 45,
    second => 30);  
#Print the formatted date and time value
print "The date and time value is ", $datetime->strftime('%A, %d %B %Y, %H-%M-%S %p'), "\n";

Output:
The following output will appear after executing the above code.

Conclusion

The ways of printing the date and time values in PERL by using localtime(), gmtime(), and DateTime module has been shown in this tutorial to help the PERL users to work with the date and time values.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.