re-add time direction
This commit is contained in:
parent
7a0b83cdc9
commit
668a2f5429
2 changed files with 21 additions and 2 deletions
|
@ -2,8 +2,13 @@
|
||||||
<time
|
<time
|
||||||
:datetime="time"
|
:datetime="time"
|
||||||
:title="localeDateString"
|
:title="localeDateString"
|
||||||
|
:class="{ warning: relativeTime.direction === 'time.in_future' }"
|
||||||
>
|
>
|
||||||
{{ $tc(relativeTime.key, relativeTime.num, [relativeTime.num]) }}
|
{{
|
||||||
|
relativeTime.direction === '' ?
|
||||||
|
$tc(relativeTime.key, relativeTime.num, [relativeTime.num]) :
|
||||||
|
$t(relativeTime.direction, [$tc(relativeTime.key, relativeTime.num, [relativeTime.num])])
|
||||||
|
}}
|
||||||
</time>
|
</time>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -50,3 +55,10 @@ export default {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss">
|
||||||
|
@import '../../_variables.scss';
|
||||||
|
time.warning {
|
||||||
|
color: var(--alertWarning, $fallback--alertWarning);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
|
@ -6,11 +6,18 @@ export const WEEK = 7 * DAY
|
||||||
export const MONTH = 30 * DAY
|
export const MONTH = 30 * DAY
|
||||||
export const YEAR = 365.25 * DAY
|
export const YEAR = 365.25 * DAY
|
||||||
|
|
||||||
|
const direction = (diff, nowThreshold) => {
|
||||||
|
if (diff < nowThreshold) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
return diff >= 0 ? 'time.in_past' : 'time.in_future'
|
||||||
|
}
|
||||||
|
|
||||||
export const relativeTime = (date, nowThreshold = 1) => {
|
export const relativeTime = (date, nowThreshold = 1) => {
|
||||||
if (typeof date === 'string') date = Date.parse(date)
|
if (typeof date === 'string') date = Date.parse(date)
|
||||||
const round = Date.now() > date ? Math.floor : Math.ceil
|
const round = Date.now() > date ? Math.floor : Math.ceil
|
||||||
const d = Math.abs(Date.now() - date)
|
const d = Math.abs(Date.now() - date)
|
||||||
let r = { num: round(d / YEAR), key: 'time.unit.years' }
|
let r = { num: round(d / YEAR), key: 'time.unit.years', direction: direction(d, nowThreshold * SECOND) }
|
||||||
if (d < nowThreshold * SECOND) {
|
if (d < nowThreshold * SECOND) {
|
||||||
r.num = 0
|
r.num = 0
|
||||||
r.key = 'time.now'
|
r.key = 'time.now'
|
||||||
|
|
Loading…
Reference in a new issue