Perl Date and Time

The most suitable way to handle date and time in P erl is by using DateTime module.

Before every operation of date and time, we need to load DateTime in memory through our script.


Perl localtime()

The localtime() function if used without any argument returns the current date and time according to the system.

Output:

Current date and time according to the system : Fri Jan 6 11:52:04 2017

Perl Create timestamp

A DateTime object displaying current date and time can be created by calling now constructor.

Example:

Output:

2017-01-06T06:29:38

We can also create a DateTime object by supplying all the details part wise like date, minute, second, etc. The perl will assume data as '0' where no detail will be passed.

Example:

Output:

2003-07-18T12:00:00

In the above output, we haven't passed any detail for minute and second part. Hence it is assumed to be zero by Perl.


Perl GMT time, gmtime()

This function works similar to localtime() function with only difference that the gmtime() returned value is localized for the standard Greenwich time zone.

Output:

Fri Jan 6 08:43:31 2017
Fri Jan 6 14:13:31 2017

Perl Epoch time

The epoch time refers to the number of seconds after a specific date and time. This specific date and time varies for different operating systems. For example, for Unix it is January 1, 1970. As all the operating systems have different epoch time, you can't assume epoch time for one system from another system.

Output:

1483686914

Perl POSIX Function strftime()

The Perl POSIX strftime() function is used to format date and time with the specifiers preceded with (%) sign. There are two types of specifiers, one is for local time and other is for gmt time zone.

Local Specifiers

SpecifierUsed ForExample
%aRepresents weekday name in shortSun
%ARepresents full weekday nameSunday
%bRepresents month name in shortJul
%BRepresents full month nameJuly
%cRepresents date and timeFri Jan 6 12:34:07 2017
%hRepresents month name in short, same as %bJul
%r12-hour format clock time6:15:30 pm
%xRepresent date12/28/12
%XRepresent time15:34:06
%ZRepresents time zone

GMT Specifiers

SpecifiersUsed ForExample
%CYear divided by 100 and written in integers (00-99)34
%dDay of the month, zero padded (01-31)33
%DRepresents MM/DD/YY07/18/17
%eDay of the month, space padded (1-31)33
%FRepresents YYYY-MM-DD2017/07/18
%gWeek based year, last two digit (00-99)05
%gWeek based year2015
%HHour in 24 hours format17
%IHour in 12hours format05
%JDay of year (001-366)365
%mMonth in decimal number (01-12)07
%MMinute (00-59)35
%nNew line character
%pAM or PMAM
%RHH:MM time in 24 hour format17:55
%SSecone (00-59)45
% tHorizontal tab character
%TISO 8601 time format (HH:MM:SS)21:45
%uISO 8601 weekday as number starting with Monday (1-7)
%Uweek number with first Sunday as first day of week one (00-53)
%VISO 8601week number (00-53)
%wWeekday as decimal number starting with Sunday(0-6)
% WWeek number with first Monday as first day of week one (00-53)17
%yLast two digits of a year (00-99)2017
% YFull year
%zISO 8601 offset from UTC in time zone (1min =1, 1 hour = 100)+100
%%A % sign%

Perl Displaying Date and Time

Sometimes we need to display a date or time in different formats. To do this, we can change the format in Perl as shown below.

Example:

Output:

2003-07-18T12:00:00
2003-07-18
18-07-2003
12:00:00
1058529600
2003
7
18
2003-07-18-12-00-00