Skip to content
Snippets Groups Projects
Unverified Commit 82c4a949 authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Fix date formatting of parameter values with time (#49046)


* Fix date formatting of parameter values with time

* Update frontend/src/metabase/parameters/utils/date-formatting.ts

Co-authored-by: default avatarKamil Mielnik <kamil@kamilmielnik.com>

* Adjust E2E test

* Fix tests

---------

Co-authored-by: default avatarKamil Mielnik <kamil@kamilmielnik.com>
parent a94d58fc
No related branches found
No related tags found
No related merge requests found
...@@ -146,7 +146,7 @@ function generateTimeValueDescription(value, bucketing, isExclude) { ...@@ -146,7 +146,7 @@ function generateTimeValueDescription(value, bucketing, isExclude) {
if (bucketing) { if (bucketing) {
return formatDateTimeWithUnit(value, bucketing, { isExclude }); return formatDateTimeWithUnit(value, bucketing, { isExclude });
} else if (m.hours() || m.minutes()) { } else if (m.hours() || m.minutes()) {
return m.format("MMMM D, YYYY hh:mm a"); return m.format("MMMM D, YYYY hh:mm A");
} else { } else {
return m.format("MMMM D, YYYY"); return m.format("MMMM D, YYYY");
} }
......
import moment from "moment-timezone"; // eslint-disable-line no-restricted-imports -- deprecated usage import moment, { type Moment } from "moment-timezone"; // eslint-disable-line no-restricted-imports -- deprecated usage
import { t } from "ttag"; import { t } from "ttag";
import _ from "underscore"; import _ from "underscore";
...@@ -126,16 +126,24 @@ function parseDateRangeValue(value: string) { ...@@ -126,16 +126,24 @@ function parseDateRangeValue(value: string) {
return { start: moment(start, true), end: moment(end, true) }; return { start: moment(start, true), end: moment(end, true) };
} }
function formatSingleDate(date: Moment) {
if (date.hours() || date.minutes()) {
return date.format("MMMM D, YYYY hh:mm A");
} else {
return date.format("MMMM D, YYYY");
}
}
export function formatRangeWidget(value: string): string | null { export function formatRangeWidget(value: string): string | null {
const { start, end } = parseDateRangeValue(value); const { start, end } = parseDateRangeValue(value);
return start.isValid() && end.isValid() return start.isValid() && end.isValid()
? start.format("MMMM D, YYYY") + " - " + end.format("MMMM D, YYYY") ? formatSingleDate(start) + " - " + formatSingleDate(end)
: null; : null;
} }
function formatSingleWidget(value: string): string | null { function formatSingleWidget(value: string): string | null {
const m = moment(value, true); const m = moment(value, true);
return m.isValid() ? m.format("MMMM D, YYYY") : null; return m.isValid() ? formatSingleDate(m) : null;
} }
function formatMonthYearWidget(value: string): string | null { function formatMonthYearWidget(value: string): string | null {
......
...@@ -31,15 +31,35 @@ const remappedField = checkNotNull(metadata.field(REMAPPED_FIELD_ID)); ...@@ -31,15 +31,35 @@ const remappedField = checkNotNull(metadata.field(REMAPPED_FIELD_ID));
describe("metabase/parameters/utils/formatting", () => { describe("metabase/parameters/utils/formatting", () => {
describe("formatParameterValue", () => { describe("formatParameterValue", () => {
const cases = [ const cases = [
{
type: "date/single",
value: "2018-01-01",
expected: "January 1, 2018",
},
{
type: "date/single",
value: "2018-01-01T12:30:00",
expected: "January 1, 2018 12:30 PM",
},
{ {
type: "date/range", type: "date/range",
value: "1995-01-01~1995-01-10", value: "1995-01-01~1995-01-10",
expected: "January 1, 1995 - January 10, 1995", expected: "January 1, 1995 - January 10, 1995",
}, },
{ {
type: "date/single", type: "date/range",
value: "2018-01-01", value: "2018-01-01T12:30:00~2018-01-10",
expected: "January 1, 2018", expected: "January 1, 2018 12:30 PM - January 10, 2018",
},
{
type: "date/range",
value: "2018-01-01~2018-01-10T08:15:00",
expected: "January 1, 2018 - January 10, 2018 08:15 AM",
},
{
type: "date/range",
value: "2018-01-01T12:30:00~2018-01-10T08:15:00",
expected: "January 1, 2018 12:30 PM - January 10, 2018 08:15 AM",
}, },
{ {
type: "date/all-options", type: "date/all-options",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment