Core\Time\DateTime::format
Converts a date and time to text, in the layout that a pattern describes.
The pattern is a string of letters, and each group of letters is one part of the value. yyyy is the year, MM is the month, dd is the day, HH is the hour, mm is the minute and ss is the second. EEEE is the name of the weekday and MMMM is the name of the month, both in English. XXX is the offset from UTC, and VV is the name of the time zone. Text inside single quotes is copied to the result unchanged.
The result shows the value in its own time zone. To print the same moment for another place, convert the value to that time zone first.
Good to know: a pattern with a mistake does not compile when you write it directly in the call. A pattern that your program builds while it runs throws a LogicError.
Parameters
$patternstringA CLDR pattern, using the pattern letters that Novis supports. A pattern is a small language, so ataintedpattern is not allowed.
Return value
Returns string. The rendered text.
Errors
LogicError$patternis not valid. It has a pattern letter that is not supported, or a quote that is not closed. A$patternwritten directly in the code is checked when the program compiles, and an error there isE0769. So only a computed$patterncan throw this error.Date::formatandTimeOfDay::formatread the same patterns but are not checked when the program compiles. There, a bad pattern written in the code also throws.
Examples
Common patterns
<?nvs// Shows how to print a date and time in a few common ways.// `format` prints the value with a pattern. Each letter in the pattern is one part of the date or time.
var $order = Core\Time\DateTime::fromIso('2024-03-05 14:07:09', 'Europe/Berlin');
// `yyyy` is the year, `MM` the month, `dd` the day, `HH` the hour, `mm` the minute and `ss` the second.// This prints 2024-03-05 14:07:09.echo $order->format('yyyy-MM-dd HH:mm:ss'), "\n";
// More letters give a longer form. `EEEE` is the name of the weekday and `MMMM` the name of the month.// This prints Tuesday, 5 March 2024.echo $order->format('EEEE, d MMMM yyyy'), "\n";
// Other characters, such as `.` and `/`, are printed as they are. This prints 05.03.2024.echo $order->format('dd.MM.yyyy'), "\n";2024-03-05 14:07:09Tuesday, 5 March 202405.03.2024Your own words and a 12 hour clock
<?nvs// Shows how to put your own words in a pattern, and how to use a 12-hour clock.
var $start = Core\Time\DateTime::fromIso('2024-11-08 19:30', 'America/New_York');
// Text in single quotes is printed as it is. Two single quotes print one quote.// `h` is the hour from 1 to 12, and `a` is AM or PM. This prints It's Friday, doors open at 7:30 PM.echo $start->format("'It''s' EEEE', doors open at' h:mm a"), "\n";
// `XXX` prints the offset from UTC. New York is 5 hours behind UTC in November.// This prints 2024-11-08T19:30:00-05:00.echo $start->format("yyyy-MM-dd'T'HH:mm:ssXXX"), "\n";
// `j` is not a pattern letter. A wrong pattern written in the code does not compile.// A pattern made while the program runs is checked by `format`, which throws a `LogicError`.string $pattern = 'j';try { echo $start->format($pattern), "\n";} catch (LogicError $error) { echo $error->message, "\n";}It's Friday, doors open at 7:30 PM2024-11-08T19:30:00-05:00Core\Time\DateTime::format(): `j` is not a pattern letter Novis supports; write `'j'` to print the letter as it isThe date in an http header
<?nvs// Shows how to write the date for an HTTP header such as `Last-Modified`.// An HTTP date is always in UTC, with short English names for the weekday and the month.
// The dates of three saved files, each with the offset of the server that saved it.array<string> $saved = ['2024-06-14T09:05:00+02:00', '2024-12-01T23:30:00-08:00', '2025-01-02T00:00:00Z'];
// `in('UTC')` converts each date to UTC before `format` prints it.// The first line this prints is Last-Modified: Fri, 14 Jun 2024 07:05:00 GMT.foreach ($saved as string $text) { var $utc = Core\Time\Instant::fromIso($text)->in('UTC'); echo 'Last-Modified: ', $utc->format("EEE, dd MMM yyyy HH:mm:ss 'GMT'"), "\n";}Last-Modified: Fri, 14 Jun 2024 07:05:00 GMTLast-Modified: Mon, 02 Dec 2024 07:30:00 GMTLast-Modified: Thu, 02 Jan 2025 00:00:00 GMT