How to get current timestamp with DateTime in Perl?
Question
How to get current timestamp with DateTime in Perl?
Getting the Current Timestamp with DateTime in Perl
The DateTime module is one of the most powerful and flexible ways to work with dates and times in Perl. It is part of the CPAN modules ecosystem and provides a rich object-oriented interface to handle date/time operations, including getting the current timestamp.
In Perl, "timestamp" can mean different things depending on context:
- Epoch timestamp: The number of seconds since the Unix epoch (January 1, 1970 UTC).
- ISO 8601 formatted datetime string, e.g.
2024-04-27T14:23:15.
Using DateTime, you can easily get both formats.
How to get current time as a DateTime object
You start by creating a new DateTime object with the current time using DateTime->now(). By default, this will be in the system's local timezone. If you want the UTC time, you can specify that explicitly.
Getting the Epoch Timestamp
The DateTime object has an epoch method that returns the Unix timestamp (seconds since 1970-01-01 00:00:00 UTC).
Example Code
use strict;
use warnings;
use DateTime;
# Get current local time as a DateTime object
my $dt = DateTime->now();
# Print DateTime object in ISO 8601 format
print "Current datetime: ", $dt->iso8601(), "\n";
# Print epoch timestamp (Unix timestamp)
print "Epoch timestamp: ", $dt->epoch(), "\n";
# You can also get UTC time by specifying time_zone
my $dt_utc = DateTime->now(time_zone => 'UTC');
print "UTC datetime: ", $dt_utc->iso8601(), "\n";
print "UTC epoch timestamp: ", $dt_utc->epoch(), "\n";
Explanation of Key Concepts
DateTime->now(): Creates a new DateTime object with the current date/time.$dt->epoch(): Returns the Unix epoch timestamp (integer seconds).$dt->iso8601(): Returns an ISO 8601 formatted date/time string.- By default, the time zone is the system local time. Use
time_zone => 'UTC'for UTC time.
The sigil $ before dt indicates a scalar variable holding a reference to a DateTime object. Calling methods on this object fetches or formats data.
Common Pitfalls
- Module availability:
DateTimeis not a core Perl module, so ensure you have installed it from CPAN. - Time zones: By default,
DateTime->now()uses your system timezone; this may differ from UTC.
Always confirm time zone behavior if your use case is timezone-sensitive. - Context awareness: When printing the epoch timestamp make sure to call
epoch()to get the numeric Unix time, instead of stringification of the object.
Version Notes
DateTime has been stable for many years and should work the same across most Perl versions 5.10 and above. The syntax used here is widely supported.
Using DateTime is a clean and robust solution to deal with current timestamps in Perl, especially for applications requiring timezone awareness or complex date/time manipulation in the future.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 185ms
Current datetime: 2025-12-30T04:46:39
Epoch timestamp: 1767069999
UTC datetime: 2025-12-30T04:46:39
UTC epoch timestamp: 1767069999
(empty)Was this helpful?
Related Questions
- How to use File::Slurp to read entire file into variable in Perl?
- How to get file modification time with Path::Tiny in Perl?
- How to use Path::Tiny touch to create empty file in Perl?
- How to use List::Util reduce for custom aggregation in Perl?
- How to compare two DateTime objects in Perl?
- How to use DateTime::Duration for time periods in Perl?