Newer
Older
import { useI18n } from '@/i18n'
// static days from sunday to saturday
// (note: month is zero-indexed)
new Date(2023, 3, 2),
new Date(2023, 3, 3),
new Date(2023, 3, 4),
new Date(2023, 3, 5),
new Date(2023, 3, 6),
new Date(2023, 3, 7),
new Date(2023, 3, 8),
function prettyWeekday(locale, weekday, format = 'long') {
return day[(weekday + 1) % 7].toLocaleString(locale, { weekday: format })
}
function leadingZero(num) {
return String(num).padStart(2, '0')
}
export function apiDate(date) {
const dateObj = new Date(date)
return `${dateObj.getFullYear()}-${leadingZero(dateObj.getMonth() + 1)}-${leadingZero(
dateObj.getDate(),
)}`
}
export function getWeekdayFromApiDate(date) {
const dateObj = new Date(date)
const weekday = dateObj.getDay()
// Date.getDay() returns 0 for Sundays, but for our APIs we need 0 for Mondays
if (weekday === 0) {
return 6
} else {
return weekday - 1
}
}
function prettyDate(locale, date) {
return new Date(date).toLocaleString(locale, {
weekday: 'long',
day: 'numeric',
month: 'long',
year: 'numeric',
})
}
function prettyTime(locale, date) {
return new Date(date).toLocaleString(locale, {
hour: '2-digit',
minute: '2-digit',
})
}
function prettyDateTime(locale, date) {
return new Date(date).toLocaleString(locale, {
weekday: 'long',
day: 'numeric',
month: 'long',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
})
}
export function prettyHours(hours) {
return hours.replace(/:00$/, '')
}
export function prettyDuration(start, end) {
const seconds = (new Date(end).getTime() - new Date(start).getTime()) / 1000
const minutes = Math.floor(seconds / 60)
let duration = minutes + 'min'
if (minutes < seconds / 60) {
duration += ' '
duration += seconds - 60 * minutes
duration += 'sec'
}
return { duration, minutes }
}
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
export function durationInSeconds(ns) {
const durationInMilliseconds = Math.floor(ns / 1000 / 1000)
return Math.floor(durationInMilliseconds / 1000)
}
export function prettyNanoseconds(ns) {
const durationInMilliseconds = Math.floor(ns / 1000 / 1000)
const seconds = Math.floor((durationInMilliseconds / 1000) % 60)
const minutes = Math.floor((durationInMilliseconds / (1000 * 60)) % 60)
const hours = Math.floor((durationInMilliseconds / (1000 * 60 * 60)) % 24)
return `${leadingZero(hours)}:${leadingZero(minutes)}:${leadingZero(seconds)}`
}
export function nanosecondsToMinutes(ns) {
return ns / 1000 / 1000 / 1000 / 60
}
export function minutesToNanoseconds(m) {
return m * 60 * 1000 * 1000 * 1000
}
export function hmsToNanoseconds(hms) {
const parts = hms.split(':')
const seconds = +parts[0] * 60 * 60 + +parts[1] * 60 + +parts[2]
return seconds * 1000 * 1000 * 1000
}
export function minutesToHm(minutes) {
const numMinutes = minutes % 60
const numHours = (minutes - numMinutes) / 60
return `${leadingZero(numHours)}:${leadingZero(numMinutes)}`
}
const methods = {
leadingZero,
apiDate,
getWeekdayFromApiDate,
prettyHours,
prettyDuration,
durationInSeconds,
prettyNanoseconds,
nanosecondsToMinutes,
minutesToNanoseconds,
hmsToNanoseconds,
minutesToHm,
}
export function usePretty() {
const { locale } = useI18n()
return {
...methods,
prettyDate(date) {
return prettyDate(locale.value, date)
prettyTime(date) {
return prettyTime(locale.value, date)
prettyDateTime(date) {
return prettyDateTime(locale.value, date)
prettyWeekday(weekday) {
return prettyWeekday(locale.value, weekday)
export default {
methods: {
...methods,
prettyDate(date) {
return prettyDate(this.$activeLocale(), date)
},
prettyTime(date) {
return prettyTime(this.$activeLocale(), date)
},
prettyDateTime(date) {
return prettyDateTime(this.$activeLocale(), date)
prettyWeekday(weekday) {
return prettyWeekday(this.$activeLocale(), weekday)