Find the difference between the two dates. Add a given number of days to the date.
DateTime class in Perl 6 defines the
– operators, which can be used in combination with either another
DateTime object or with the
Let us first find the difference between the two given dates:
my $date1 = DateTime.new('2017-12-31T23:59:50');
my $date2 = DateTime.new('2018-01-01T00:00:10');
say $date2 - $date1; # 20
The type of the
$date2 - $date1 expression is
Duration. In the numeric context, it returns the number of seconds. Therefore, there are 20 seconds between our dates at hand.
Duration object can also be used instead of the second
For example, increase the given date by two minutes:
my $now = DateTime.now();
my $when = $now + Duration.new(120);
say "$now -> $when";
Or learn what were the date and time a week ago:
my $back = $now - Duration.new(3600 * 24 * 7);
In the current design of the language, the constructor of the
Duration class needs a number of seconds.