Skip to content

Core\Time\Date

A date with no time of day and no zone, such as a birthday or a due date. Core\Time\Date::at builds one from a year, a month and a day. plus and minus move it by days, months or years, and format turns it into text.

A Date is a year, a month and a day. It has no time of day and no zone. Date::fromIso reads a date written as YYYY-MM-DD, such as 2024-03-01. Date::parse reads a date that is written another way, with a pattern such as dd.MM.yyyy. Date::at builds one from three numbers, and ->date() gives the date of a DateTime. A date that does not exist throws an error when it is built. A date written as a string literal in Date::fromIso is checked when the program compiles.

year(), month() and day() return the three fields as numbers. weekday() returns a Core\Time\Weekday case, dayOfYear() returns the day of the year from 1, and isLeapYear() checks the year. DateTime::at($date, TimeOfDay::at(0, 0), $zone) returns the first moment of the date in a zone.

plus and minus move a date by Core\Unit::Day or a larger unit. A smaller unit throws an error. Moving by months keeps the day inside the new month, as DateTime::plus does. format takes the date letters of the patterns DateTime::format uses. A time letter or a zone letter does not compile, because a Date has no time and no zone. <, >, <=, >= and compareTo order two dates. toIso() returns the date as text such as 2024-01-31, and fromIso reads that text back.

startOf and endOf return the first and the last day of the week, month, quarter or year of a date. A week runs from Monday to Sunday. difference counts the whole days, weeks, months, quarters or years from one date to another. next and previous return the nearest date after or before a date that is on a given Core\Time\Weekday. daysInMonth() returns the number of days in the month, and quarter() returns 1 to 4. isoWeek() returns the ISO 8601 week number, and isoWeekYear() returns the year that this week belongs to. Date::range returns the list of dates from one date to another, with one date for each day, week, month, quarter or year.

<?nvs
var $d = Core\Time\Date::fromIso('2024-01-31');
echo $d->format('yyyy-MM-dd'), "\n";
echo $d->format('EEEE, d MMMM'), "\n";
echo $d->plus(1, Core\Unit::Month)->format('yyyy-MM-dd'), "\n";
echo $d->minus(1, Core\Unit::Day)->format('EEE d MMM'), "\n";
echo $d->with({ month: 3 })->format('yyyy-MM-dd'), "\n";
echo $d->compareTo(Core\Time\Date::at(2024, 2, 1)), "\n";
try {
Core\Time\Date::at(2023, 2, 29);
} catch (RuntimeError $bad) {
echo "2023-02-29 does not exist\n";
}
echo Core\Time\DateTime::fromIso('2024-03-01 23:00', 'UTC')->date()->format('d/M/yyyy'), "\n";
echo $d->year(), ' ', $d->month(), ' ', $d->day(), "\n";
echo $d->dayOfYear(), ' ', $d->isLeapYear() ? 'leap year' : 'not a leap year', "\n";
var $midnight = Core\Time\TimeOfDay::at(0, 0);
echo Core\Time\DateTime::at($d, $midnight, 'Europe/Berlin')->format('yyyy-MM-dd HH:mm XXX'), "\n";
Output
2024-01-31
Wednesday, 31 January
2024-02-29
Tue 30 Jan
2024-03-31
-1
2023-02-29 does not exist
1/3/2024
2024 1 31
31 leap year
2024-01-31 00:00 +01:00

Returned by

You get a Core\Time\Date from these methods:

Members

25 members.

MemberSignature
atat(int $year, uint $month, uint $day): Core\Time\Date
fromIsofromIso(string $text): Core\Time\Date
parseparse(string $text, string $format): Core\Time\Date
rangerange(Core\Time\Date $from, Core\Time\Date $to, { step?: Core\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year }): array<Core\Time\Date>
formatformat(string $pattern): string
plusplus(int $count, Core\Unit $unit): Core\Time\Date
minusminus(int $count, Core\Unit $unit): Core\Time\Date
withwith({ year?: int, month?: uint, day?: uint }): Core\Time\Date
compareTocompareTo(Core\Time\Date $other): int
toIsotoIso(): string
weekdayweekday(): Core\Time\Weekday
dayOfYeardayOfYear(): uint
isLeapYearisLeapYear(): bool
startOfstartOf(Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit): Core\Time\Date
endOfendOf(Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit): Core\Time\Date
differencedifference(Core\Time\Date $other, Core\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit): int
nextnext(Core\Time\Weekday $w): Core\Time\Date
previousprevious(Core\Time\Weekday $w): Core\Time\Date
daysInMonthdaysInMonth(): uint
quarterquarter(): uint
isoWeekisoWeek(): uint
isoWeekYearisoWeekYear(): int
yearyear(): int
monthmonth(): uint
dayday(): uint