In Date§

See primary documentation in context for method DateTime

multi method DateTime(Date:U: --> DateTime:U)
multi method DateTime(Date:D: --> DateTime:D)

Converts the invocant to DateTime

say Date.new('2015-12-24').DateTime; # OUTPUT: «2015-12-24T00:00:00Z␤»
say Date.DateTime;                   # OUTPUT: «(DateTime)␤»

In DateTime§

See primary documentation in context for method DateTime

method DateTime(--> DateTime)

Returns the invocant.

say DateTime.new("2012-02-29T12:34:56.946314Z").DateTime;
# OUTPUT: «2012-02-29T12:34:56.946314Z␤»
say DateTime.DateTime;
# OUTPUT: «(DateTime)␤»

In Instant§

See primary documentation in context for method DateTime

method DateTime(Instant:D: --> DateTime:D)

Coerces the invocant to DateTime.

say now.DateTime;  # OUTPUT: «2017-05-09T14:02:58.147165Z␤»

In Str§

See primary documentation in context for method DateTime

method DateTime(Str:D:)

Available as of the 2020.05 release of the Rakudo compiler.

Coerces a Str to a DateTime object, provided the string is properly formatted. DateTime(Str) also works.

Examples:

say ('2012-02-29T12:34:56Z').DateTime.hour; # OUTPUT: «12␤»
say DateTime('2012-02-29T12:34:56Z').hour;  # OUTPUT: «12␤»

Since Rakudo release 2022.07, it is also possible to just specify a "YYYY-MM-DD" string to indicate midnight on the given date.

say "2023-03-04".DateTime;  # OUTPUT: «2023-03-04T00:00:00Z␤»