September 2017
Beginner
402 pages
9h 52m
English
The usage of the DateTime data type is very similar to working with the Date type. In the DateTime objects, new fields to deal with time appear. We can see that in the following code snippet:
my $dt = DateTime.new( year => 2017, month => 7, day => 19, hour => 1, minute => 46, second => 48);say $dt; # 2017-07-19T01:46:48Zsay $dt.year; # 2017say $dt.month; # 7say $dt.day; # 19say $dt.hour; # 1say $dt.minute; # 46say $dt.second; # 48
To create a new DateTime object and set it to the current moment, use the now constructor, as shown here:
my $dt = DateTime.now;say $dt; # 2017-07-19T01:44:00.301537+02:00
The hh-mm-ss and yyyy-mm-dd methods generate formatted strings for time and date:
say $dt.yyyy-mm-dd; # 2017-07-19 ...
Read now
Unlock full access