When using the custom formatter for a datetime range, the start and end dates are compared as timestamps to determine whether to display both. If the chosen custom format only includes a date, but the end date includes a different time, the output is (for example): March 1, 2018 - March 1, 2018.

CommentFileSizeAuthor
#2 custom-formatter-equality-2949108-1.patch934 bytesbyrond

Comments

byrond created an issue. See original summary.

byrond’s picture

StatusFileSize
new934 bytes

This patch uses the formatted date to determine whether both start and end dates need to be displayed.

mpdonadio’s picture

Thanks for reporting a bug and posting a patch.

Can you merge your work into #2823847: DateRange formatters should compare rendered dates instead of raw timestamps? I think you can take your formatter code and that patch's test, and we should be pretty close. We started this a while ago, and I want to make sure everyone gets credit.

Thanks!

Quick look:

+++ b/core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeCustomFormatter.php
@@ -51,7 +51,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
-        if ($start_date->getTimestamp() !== $end_date->getTimestamp()) {
+        if ($this->buildDate($start_date) !== $this->buildDate($end_date)) {

Since building date strings is an expensive operation, I think we want to save them into a variable so we don't need to rebuild for the render array.