Fix strict relative dates showing "incorrect" (#14390)

* Set rounding method of relative formatted date to floor when strict option is selected

* Add round fn as an option

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
Gerard Lamusse
2022-07-12 15:48:45 +02:00
committed by GitHub
parent de9d06a274
commit fb90db9872
3 changed files with 28 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ interface Props {
format?: string;
relative?: boolean;
strict?: boolean;
round?: string;
suffix?: boolean;
}
@@ -23,6 +24,7 @@ const props = withDefaults(defineProps<Props>(), {
format: 'long',
relative: false,
strict: false,
round: 'round',
suffix: true,
});
@@ -50,6 +52,7 @@ const relativeFormat = (value: Date) => {
const fn = props.strict ? localizedFormatDistanceStrict : localizedFormatDistance;
return fn(value, new Date(), {
addSuffix: props.suffix,
roundingMethod: props.round,
});
};

View File

@@ -123,6 +123,26 @@ export default defineDisplay({
schema: {
default_value: false,
},
},
{
field: 'round',
name: '$t:displays.datetime.round',
type: 'string',
meta: {
width: 'half',
interface: 'select-dropdown',
options: {
choices: [
{ text: '$t:displays.datetime.down', value: 'floor' },
{ text: '$t:displays.datetime.nearest', value: 'round' },
{ text: '$t:displays.datetime.up', value: 'ceil' },
],
},
note: '$t:displays.datetime.round_note',
},
schema: {
default_value: 'round',
},
}
);
}

View File

@@ -1768,6 +1768,11 @@ displays:
strict: Strict
strict_label: Use strict units
strict_note: Removes words like 'almost', 'over', 'less than'
round: Round
round_note: Choose a rounding function to use
down: Down
nearest: Nearest
up: Up
file:
file: File
description: Display files