Fix date formatting to use UTC

This commit is contained in:
Deluan
2023-05-24 14:47:51 -04:00
parent e38a690632
commit ba067667c9
6 changed files with 38 additions and 38 deletions
+14
View File
@@ -24,3 +24,17 @@ export const formatDuration = (d) => {
return `${days > 0 ? days + ':' : ''}${f}`
}
export const formatFullDate = (date) => {
const dashes = date.split('-').length - 1
let options = {
year: 'numeric',
timeZone: 'UTC',
...(dashes > 0 && { month: 'short' }),
...(dashes > 1 && { day: 'numeric' }),
}
if (dashes > 2 || (dashes === 0 && date.length > 4)) {
return ''
}
return new Date(date).toLocaleDateString(undefined, options)
}
+17 -1
View File
@@ -1,4 +1,4 @@
import { formatBytes, formatDuration } from './formatters'
import { formatBytes, formatDuration, formatFullDate } from './formatters'
describe('formatBytes', () => {
it('format bytes', () => {
@@ -31,3 +31,19 @@ describe('formatDuration', () => {
expect(formatDuration(day + minute + 0.6)).toEqual('1:00:01:01')
})
})
describe('formatFullDate', () => {
beforeAll(() => {
const toLocaleString = Date.prototype.toLocaleString
// eslint-disable-next-line no-extend-native
Date.prototype.toLocaleString = function (locale = 'en-US', ...args) {
return toLocaleString.call(this, locale, ...args)
}
})
it('format bytes', () => {
expect(formatFullDate('2011')).toEqual('2011')
expect(formatFullDate('2011-06')).toEqual('Jun 2011')
expect(formatFullDate('1985-01-01')).toEqual('Jan 1, 1985')
expect(formatFullDate('199704')).toEqual('')
})
})