Skip to content
Related Articles

Related Articles

Perl | Date and Time

View Discussion
Improve Article
Save Article
  • Last Updated : 21 Nov, 2019

Perl is one of the most feature-rich programming languages. Perl is portable and cross-platform. Perl can run on over 100 platforms with highly integrated text processing ability. Perl contains the features of different languages like C, sed, awk, and sh etc. which makes the Perl more useful and productive.

Perl is an easy-to-use language. It is intended to be efficient and complete rather than elegant and minimal. Perl supports some major programming paradigms together with object-oriented, procedural, and practical.
Date and Time in Perl can be handled by using a predefined module of Perl called DateTime module. DateTime is a class for the representation of various combinations of dates and times. DateTime module in Perl represents the New Style calendar, extended backward in time before its creation (in 1582).
This is typically referred to as the “proleptic Gregorian calendar”.
In this calendar, the first day of the calendar (the epoch), is the first day of year 1, which corresponds to the date which was (incorrectly) believed to be the birth of Jesus Christ.

Syntax: use DateTime;


localtime() function in Perl returns the current date and time of the system, if called without passing any argument.

$datetime = localtime();  
print "Local Time of the System : $datetime\n"


Local Time of the System : Fri Nov 15 07:21:05 2019

Creating a Timestamp

A timestamp in Perl is used to display the current Date and Time of the day. Data displayed by timestamp is sometimes accurate to a fraction of seconds.
Timestamp can be created by creating a DateTime object and then calling the now constructor.

use DateTime;
# Creation of Timestamp
my $datetime = DateTime->now;  
print "$datetime\n";  

A DateTime object can also be created by providing all the details part wise like date, hour, minute, second, etc. Default value for variables with no value passed is ‘0‘.

use DateTime; 
# Assigning values to variable
$datetime = DateTime->new(  
    day        => 18,  
    month      => 7,  
    year       => 2003,  
    hour       => 12,

Finding GMT Time

GMT stands for Greenwich Mean Time, which is the mean solar time at the Royal Observatory in Greenwich, London. GMT is the same during all the year and is not affected by Daylight Saving Time(Summer Time) clock changes.
Perl provides a predefined function for calculation and representation of GMT, which is gmtime(). This function works similar to localtime() function but the only difference is that the time values are localized for the Greenwich time zone only.

# Using function gmtime()
$datestring = gmtime();
# Printing GMT time
print "Date and time in GMT: $datestring\n";


Date and time in GMT: Fri Nov 15 11:10:05 2019

Formatting Date and Time

localtime() function can also be used to print date and time in various formats as per the user’s requirement. This formatting can be easily done by using printf() function.

# Assigning values to variables
($sec, $min, $hour) = localtime();
printf("Time Format - HH:MM:SS\n");
# Formatting the time representation
        $hour, $min, $sec);


Time Format - HH:MM:SS

Epoch Time

Epoch time refers to the number of seconds passed after a specific date and time. The specific date and time used to calculate epoch time vary from OS to OS. For example, for POSIX or UNIX systems, this date is January 1, 1970. Since this time varies from system to system, one cannot assume epoch time for any system.

# Calculating epoch time
$epoch = time();  
print "$epoch\n";  



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!