< Perl Tutorial

Perl date and time

← Previous Next →


Perl Date & Time

Here we will get to know the Time and Date objects and functions that are available to us via Perl. We will use these functions to get, set as well as manipulate date and time.

Current Date & Time

We will use the localtime() function to get the details of the current date and time. By default, this function gives details of date and time with respect to the current Time Zone in which our machine is. This function returns a 9-element list containing the details of the current date and time. The 9-element list alongwith the description of each element is as follows:

  • sec - seconds of a minute in the range of 0 to 61
  • min - minutes of hour in the range of 0 to 59
  • hour - hours of a day in the range of 0 to 24
  • mday - days of a month in the range of 1 to 31
  • mon - months of year in the range of 0 to 11
  • year - year since 1900
  • wday - days since Sunday
  • yday - days since January 1st
  • isdst - hours of Daylight Savings Time

The following code shows an example of how to use the localtime() function to get the current date and time:

$datestring = localtime();
print "Local date and time $datestring\n";

The above code produces the following output:

Local date and time Sat Feb 16 06:50:45 2013

Greenwich Mean Time

Here we make use of the function gmtime() instead of localtime(). This value returns the current Greenwich Mean Time from the GMT Zone. There is no Daylight Savings at GMT so the value of isdst is always 0.

The following code demonstrates the use of gmtime() function:

$datestring = gmtime();
print "GMT date and time $datestring\n";

The output of the above code is:

GMT date and time Sat Feb 16 13:50:45 2013

Epoch Time

Epoch time means the number of seconds that have elapsed since January 1, 1970. Here make use of the time() function instead of localtime() and gmtime().

The following code shows the usage of time() function:

$epoc = time();

print "Number of seconds since Jan 1, 1970 - $epoc\n";

The above code produces the following output:

Number of seconds since Jan 1, 1970 - 1361022130

POSIX Function

A special POSIX function by the name of strftime() is available in Perl which lets us format date and time according to our needs. The following table gives the details about the specifiers that can be used with this function:

 Specifier Replaced by  Example 
 %a Abbreviated week day name Thu
 %A Full week day name  Thursday 
 %b Abbreviated month name  Aug 
 %B Full month name  August 
 %c Date and Time representation  Thu Aug 23 14:55:02 2001 
 %C Year divided by hundred and truncated to integer  20 
 %d Day of the month (01-31)  23 
 %D Short MM/DD/YY date  08/23/01 
 %e Day of the month (1-31)  23 
%F Short YYYY-MM-DD date 2005-08-23
%g Week based year(00-99) 01
%G Week-based year 2001
%h Abbreviated month name Aug
%H Hour in 24h format 14
%I Hour in 12h format 02
%j Day of the year(001-366) 235
%m Month as a decimal number(01-12) 08
%M Minute 55
%n New-line character('\n') -
%p AM or PM designation PM
%r 12-hour clock time 02:55:02 pm
%R 24 Hour HH:MM time 14:55
%S Second 02
%t Horizontal tab character('\t') -
%T ISO 8601 time format(HH:MM:SS) 14:55:02
%u ISO 8601 weekday as number with Monday as 1 4
%U Week number with Sunday as first day of week one 33
%V ISO 8601 week number 34
%w Weekday as a decimal number with Sunday as 0 4
%W Week number with first Monday as the first day of week one 34
%x Date representation 08/23/01
%X Tme representation 14:55:02
%y Year, last two digits 01
%Y Year 2001
← Previous Next →