What are date and time in Perl?

Localtime

The in-built localtime() function in Perl displays the following in local time:

Variable

Meaning

sec

seconds

min

minutes

hour

hours

mday

day of month

month

month

year

year since 1900

wday

day of week

yday

day of year

isdst

hours of daylight saving time

Below is an example of how you can use the localtime() function:

$timedisplay = localtime();
print "local time: $timedisplay\n";

Gmtime

The gmtime() function in Perl standardizes the time to Greenwich Mean Time while also keeping the daylight saving hours to 0, as there are no daylight savings in GMT. Below is an example of how you can use gmtime():

$timedisplay = gmtime();
print "local time: $timedisplay\n";

Epoch time

The time() function in Perl returns the number of seconds elapsed since Jan 1, 1970. Following is an example of how you can use the time() function:

$timedisplay = time();
print "Seconds elapsed: $timedisplay\n";

Strftime

The strftime() function in Perl can be used to format time. The table below explains what each specifier in the function stands for:

Specifier

Meaning

%a

name of the weekday in abbreviated format

%A

name of weekday

%b

name of the month in abbreviated format

%B

name of month

%c

date and time representation

%d

day of month, padded with zero in case of single digits

%D

MM/DD/YY

%e

day of month, not padded

%H

hour in 24-hour format

%I

hours in 12-hour format

%j

day of year

%M

month

%m

minute

%p

AM or PM

%r

clock time displayed in 12-hours

%R

clock time displayed in 24-hours

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved