Perl time and date

In this chapter we introduce you to the processing of time and date in the Perl language.

The functions that process time in Perl are as follows:

  • 1, time () function: returns the number of seconds since January 1, 1970
  • 2, localtime () function: get local time zone time
  • 3, gmtime () function: get Greenwich time

current time and date

Next let's take a look at the localtime() function, which returns the current time and date without parameters.

The following 9 symbols represent different time and date parameters:

sec, # seconds, 0 to 61
Min, # minutes, 0 to 59
Hour, # hours, 0 to 24
Mday, # days, 1 to 31
Mon, # month, 0 to 11
Year, # years, starting at 1900
Wday, # Day of the week, 0-6,0 means Sunday 
Yday, # The first few days of the year, 0-364,365 
Isdst # true if daylight saving time is valid

The example is demonstrated as follows:


#!/usr/bin/perl @months = qw( January February March April May June July August September October November December); @days = qw(Sunday Monday Tuesday Wednesday Wednesday Thursday Friday); ($sec, $min,$hour,$mday,$mon,$year ,$wday,$yday,$isdst) = localtime(); print "$mday $months[$mon] $days[$wday]\n";

The above example execution output is:

12 June Sunday

If you call localtime() directly, it returns the time when the system set the time zone in the current period. The example is as follows:


#!/usr/bin/perl $datestring= localtime (); print "Time and date are:$datestring\n";

The above example execution output is:

Time and date are:Sun Jun 12 11:27:31 2016

Greenwich Mean Time (GMT)


function gmtime() is similar to localtime() , but it returns the standard Greenwich time.


#!/usr/bin/perl $local_datestring= localtime (); print "The local time date is:$local_datestring\n"; $gmt_datestring= gmtime (); print "GMT date and time is:$gmt_datestring\n";

The above example execution output is:

Local time dates are:Sun Jun 12 11:32:14 2016
GMT Time and date are:Sun Jun 12 03:32:14 2016

From the example we can see that the time between China and Greenwich Mean Time is 8 hours.

welookups is optimized for learning.© welookups. 2018 - 2020 All Right Reserved and you agree to have read and accepted our term and condition.