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()
localtime()
function in
Perl returns the current date and time of the system, if called without passing any argument.
Perl
#!/usr/local/bin/perl
$datetime = localtime();
print "Local Time of the System : $datetime\n";
Output:
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.
perl
#!/usr/bin/perl
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'.
Perl
#!/usr/bin/perl
use DateTime;
# Assigning values to variable
$datetime = DateTime->new(
day => 18,
month => 7,
year => 2003,
hour => 12,
);
print"$datetime\n";
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.
Perl
#!/usr/local/bin/perl
# Using function gmtime()
$datestring = gmtime();
# Printing GMT time
print "Date and time in GMT: $datestring\n";
Output:
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.
perl
#!/usr/local/bin/perl
# Assigning values to variables
($sec, $min, $hour) = localtime();
printf("Time Format - HH:MM:SS\n");
# Formatting the time representation
printf("%02d:%02d:%02d",
$hour, $min, $sec);
Output:
Time Format - HH:MM:SS
11:38:06
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.
perl
#!/usr/local/bin/perl
# Calculating epoch time
$epoch = time();
print "$epoch\n";
Similar Reads
SQLite Date and Time
In the world of database management, understanding how to handle dates and times is important. SQLite is a popular relational database management system that offers a robust set of functions and features for working with date and time data. From storing timestamps to performing date calculations, SQ
5 min read
MySQL Insert Date Time
In today's world, working with data is now a data-to-data activity, so therefore managing data with proper data and time is also crucial. MySQL provides functionalities to handle data and time properly in the database. Understanding how to insert data and time into MySQL database with functions prov
4 min read
Dates and Times in Objective-C
In Objective-C, dates and times are represented using the NSDate and NSDateFormatter classes. NSDate class represents a single point in time. It stores an absolute point in time, independent of any particular calendar or time zone. You can use NSDate to represent a date and time, or just a date or t
4 min read
Adding Dates and Times in Python
Prerequisite: Python â datetime.tzinfo() Python supports datetime module to perform manipulations for date and time. Datetime module allows the user to perform various date and time-related arithmetic operations, extraction of various time frames and formatting of output in different formats. Object
3 min read
Handle date and time columns using R
Managing date and time data is a crucial aspect of data analysis, as many datasets involve temporal information. R Programming Language is used for statistical computing and data analysis and provides several functions and packages to handle date and time columns effectively. Here we cover various t
11 min read
How to get date and timestamp in Scala?
In this article, we will learn how to get date and timestamp in scale. to get the current date and timestamp we can use java.time package. This package provides data and time features that are introduced in Java 8. We can use the LocalDateTime class to get the date and time and we can format it acco
2 min read
How to Get Current Date and Time in SQL?
Managing date and time efficiently is crucial for any database system. SQL provides built-in functions to retrieve the current date and time, which are especially useful for applications involving logging, reporting, and auditing. In this article, we will explain the three main SQL functions to fetc
4 min read
AngularJS | date Filter
AngularJS date filter is used to convert a date into a specified format. When the date format is not specified, the default date format is 'MMM d, yyyy'. Syntax: {{ date | date : format : timezone }} Parameter Values: The date filter contains format and timezone parameters which is optional.Some com
2 min read
Parsing Time in Golang
Parsing time is to convert our time to Golang time object so that we can extract information such as date, month, etc from it easily. We can parse any time by using time.Parse function which takes our time string and format in which our string is written as input and if there is no error in our form
2 min read
Difference between Time and DateTime in Ruby
In Ruby on Rails, managing dates and times is crucial for many applications, whether for event scheduling, time tracking, or handling time zones. Ruby provides two primary classes for managing dates and times: Time and DateTime. Both have unique attributes and use cases, and it's essential to unders
5 min read