feat(dashboard): timestamp parsing (#461)

This commit is contained in:
pulchra mentis
2022-06-17 23:03:07 -04:00
committed by spiral
parent 62c5c3865a
commit 33b77470ee
4 changed files with 36 additions and 7 deletions

View File

@@ -0,0 +1,26 @@
import moment from 'moment'
const timestampRegex = /<t:(\d{10})(:[tTdDfFR])?>/g
const parseTimestamps = (html: string) => {
return html.replaceAll(
timestampRegex,
(match, p1, p2) => {
const timestamp = moment.unix(parseInt(p1))
const format: string = p2 ? p2[1] : 'f'
if (format !== 'R') {
let dateTimeFormatOptions = {
t: 'HH:mm',
T: 'HH:mm:ss',
d: 'DD/MM/YYYY',
D: 'DD MMMM YYYY',
f: 'DD MMMM YYYY HH:mm',
F: 'dddd, DD MMMM YYYY HH:mm',
}[format]
return timestamp.format(dateTimeFormatOptions)
}
return timestamp.fromNow()
},
)
}
export default parseTimestamps