@northscaler/service-support
Version:
Artifacts that assist in writing service layers effectively
38 lines (33 loc) • 752 B
JavaScript
const Enumeration = require('@northscaler/enum-support')
/**
* Enumeration object of date formats.
* Values are:
* * `ISO_8601`
* * `UNIX_MILLISECONDS`
* * `UNIX_SECONDS`
*/
const DateFormat = Enumeration.new({
name: 'DateFormat',
values: [
'ISO_8601',
'UNIX_MILLISECONDS',
'UNIX_SECONDS'
]
}, {
format (date) {
if (!date) return date
if (!(date instanceof Date)) return date
switch (this) {
case DateFormat.ISO_8601:
return date.toISOString()
case DateFormat.UNIX_MILLISECONDS:
return date.getTime()
case DateFormat.UNIX_SECONDS:
return date.getTime() / 1000
default:
throw new DateFormat.$ERROR$()
}
}
})
module.exports = DateFormat