Core\Time\DateTime::date
$dateTime->date(): Core\Time\DateReturns the calendar day of a date and time, as a Core\Time\Date.
The result has the year, the month and the day. It has no time of day and no time zone. Use it when only the day matters, for example to count orders for each day or to compare a value with a due date.
Good to know: date reads the day in the time zone of the value. One moment can be a different day in another place. 20:00 on 31 December in New York is already 1 January in Tokyo. To get the day for a certain place, convert the value to that time zone first and then call date.
Return value
Returns Core\Time\Date. The Date component.
Examples
The day of a date and time
<?nvs// Shows how to take the calendar day out of a date and time.
var $meeting = Core\Time\DateTime::fromIso('2024-03-05 14:30', 'Europe/Berlin');
// `date` returns a `Core\Time\Date` with only the year, month and day.// This prints 2024-03-05.var $day = $meeting->date();echo $day->format('yyyy-MM-dd'), "\n";
// The time of day is gone, so the result is equal to a date built from the same three numbers.// This prints 0.echo $day->compareTo(Core\Time\Date::at(2024, 3, 5)), "\n";
// The result has the methods of a `Date`. This prints 2024-03-12.echo $day->plus(1, Core\Unit::Week)->format('yyyy-MM-dd'), "\n";2024-03-0502024-03-12The day depends on the time zone
<?nvs// Shows that one moment can be a different day in another time zone.
// A call starts at 20:00 on 31 December 2024 in New York.var $call = Core\Time\DateTime::fromIso('2024-12-31 20:00', 'America/New_York');
// `date` reads the day in the time zone of the value. This prints 2024-12-31.echo $call->date()->format('yyyy-MM-dd'), "\n";
// At that moment it is 10:00 on the next day in Tokyo. This prints 2025-01-01.var $tokyo = $call->toInstant()->in('Asia/Tokyo');echo $tokyo->date()->format('yyyy-MM-dd'), "\n";2024-12-312025-01-01Orders per day in the shop time zone
<?nvs// Shows how to count a shop's orders for each day in the shop's own time zone.
// The server saves each order time in UTC.array<string> $saved = [ '2024-06-03T08:15:00Z', '2024-06-03T17:40:00Z', '2024-06-03T22:30:00Z', '2024-06-04T09:05:00Z', '2024-06-04T23:10:00Z',];var $shop = Core\Time\Zone::of('Europe/Berlin');
// Berlin is two hours ahead of UTC in June. So an order at 22:30 UTC is at 00:30 on the next day in Berlin.// This prints 2 orders for 3 June, 2 for 4 June and 1 for 5 June.var $day = Core\Time\Instant::fromIso($saved[0])->in($shop)->date();int $count = 0;foreach ($saved as string $text) { var $next = Core\Time\Instant::fromIso($text)->in($shop)->date(); if ($next->compareTo($day) != 0) { echo 'Orders on ', $day->format('d MMMM'), ': ', $count, "\n"; $day = $next; $count = 0; } $count = $count + 1;}echo 'Orders on ', $day->format('d MMMM'), ': ', $count, "\n";Orders on 3 June: 2Orders on 4 June: 2Orders on 5 June: 1