📘 Current date and time in Perl 6

📘 Current date and time in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


Print current date and time as an epoch and in a human-readable format.

In Perl 6, the time function returns the current time as the Unix epoch:

say time;

The output is something like this: 1495785518.

For manipulating dates and times, use the built-in DateTime class:

say DateTime.now;

The date is now in a more human-readable format, although still is overloaded with many details: 2017-05-26T10:02:20.500209+02:00.

To access the separate elements of date and time, use the methods on the variable of the DateTime class:

my $dt = DateTime.now;

say $dt.day;   # 26
say $dt.month; # 5
say $dt.year;  # 2017
say $dt.hour;  # 10
say $dt.minute; # 9
say $dt.second; # 5.55802702903748

The meaning of all the elements is quite straightforward.

All the values except seconds are integer. For the seconds, you may want to take the integer part only:

say $dt.second.Int;

Leave a Reply