📘 Formatted date in Perl 6

Print the current date in an even better format.

In Perl 6, there is a built-in DateTime class. It is equipped with a few useful methods, so there’s no need to use external modules for many standard tasks. Save the current moment in the $now variable:

my $now = DateTime.now;

The easiest thing is to print the proper time using a dedicated method:

say $now.hh-mm-ss; # 22:37:16

To achieve a more granular control over both the date and time parts, use the formatter attribute of the constructor together with methods like day and month to get separate parts of the date and time (see Task 87, Current date and time): 

my $now = DateTime.now(
    formatter => {
        sprintf '%02d.%02d.%04d, %02d:%02d', 
        .day, .month, .year, .hour, .minute

say $now; # 14.10.2017, 22:41

The formatting string '%02d.%02d.%04d, %02d:%02d' uses the standard POSIX printf format. The sprintf function forms a string that is returned when a DateTime object (the $now variable, in our case) is stringified. For instance, it is used when the variable is interpolated in a string:

say "Current date and time: $now.";

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s