Skip to content

Instantly share code, notes, and snippets.

@greenn-lab
Last active May 17, 2021 05:12
Show Gist options
  • Save greenn-lab/10d896a373fd6868e3f4e13876f61fd2 to your computer and use it in GitHub Desktop.
Save greenn-lab/10d896a373fd6868e3f4e13876f61fd2 to your computer and use it in GitHub Desktop.
Javascript Simple Date Formatter ( https://github.com/greenn-lab/nal )
(function() {
const i18n = {
am: '오전',
pm: '오후',
pattern: {
default: 'yyyyMMddHHmmss'
}
}
const DP = Date.prototype
const D2 = () => /\d\d/
const D12 = () => /\d\d?/
const units = {
yyyy: [() => /\d{4}/, DP.setFullYear, DP.getFullYear],
yy: [
D2,
function (v) {
this.setFullYear(
Math.floor(new Date().getFullYear() / 1000) * 1000 + v
)
},
function () {
return this.getFullYear().toString().substring(2)
}
],
MM: [
D2,
function (v) {
this.setMonth(v - 1)
},
function () {
return `0${this.getMonth() + 1}`.substr(-2)
}
],
M: [
D12,
function (v) {
this.setMonth(v - 1)
},
function () {
return this.getMonth() + 1
}
],
dd: [
D2,
DP.setDate,
function () {
return `0${this.getDate()}`.substr(-2)
}
],
d: [
D12,
DP.setDate,
function () {
return this.getDate()
}
],
HH: [
D2,
function (v) {
this.setHours(this.getHours() + Number(v))
},
function () {
return `0${this.getHours()}`.substr(-2)
}
],
H: [
D12,
function (v) {
this.setHours(this.getHours() + Number(v))
},
function () {
return this.getHours()
}
],
hh: [
D2,
function (v) {
this.setHours(this.getHours() + Number(v))
},
function () {
return `0${this.getHours() % 12 || 12}`.substr(-2)
}
],
h: [
D12,
function (v) {
this.setHours(this.getHours() + Number(v))
},
function () {
return this.getHours() % 12 || 12
}
],
mm: [
D2,
function (v) {
this.setMinutes(this.getMinutes() + Number(v))
},
function () {
return `0${this.getMinutes()}`.substr(-2)
}
],
m: [
D12,
function (v) {
this.setMinutes(this.getMinutes() + Number(v))
},
function () {
return this.getMinutes()
}
],
ss: [
D2,
function (v) {
this.setSeconds(this.getSeconds() + Number(v))
},
function () {
return `0${this.getSeconds()}`.substr(-2)
}
],
s: [
D12,
function (v) {
this.setSeconds(this.getSeconds() + Number(v))
},
function () {
return this.getSeconds()
}
],
SSS: [
() => /\d{1,3}/,
DP.setMilliseconds,
function () {
return `00${this.getMilliseconds()}`.substr(-3)
}
],
SS: [
D2,
DP.setMilliseconds,
function () {
return `0${this.getMilliseconds()}`.substr(-2)
}
],
S: [
() => /\d/,
DP.setMilliseconds,
function () {
return this.getMilliseconds().toString().substring(0, 1)
}
],
a: [
() => new RegExp([i18n.am, i18n.pm].join('|')),
function (v) {
v = i18n.pm === v ? 12 : 0
this.setHours(this.getHours() + v)
},
function () {
return [i18n.am, i18n.pm][Math.floor(this.getHours() / 12)]
}
]
}
const format = (date, pattern = i18n.pattern.default) => {
return pattern.replace(
new RegExp(`(${Object.keys(units).join('|')})`, 'g'),
function (unit) {
return units[unit][2].call(date)
}
)
}
const parse = (text, pattern = i18n.pattern.default) => {
if (!text) {
throw new Error('required text')
}
const date = new Date(0, 0, 1)
const joinUnitKeys = Object.keys(units).join('|')
const effectedRegExp = new RegExp(joinUnitKeys)
let patternIndex = 0
let textIndex = 0
let matched
while (
(matched = effectedRegExp.exec(pattern.substring(patternIndex)))
) {
const key = matched[0]
patternIndex += matched.index + key.length
const [unit, setter] = units[key]
if (unit) {
textIndex += matched.index
const [value] = unit().exec(text.substring(textIndex)) || []
if (value) {
setter.call(date, value)
textIndex += value.length
}
}
}
return date
}
Date.prototype.i18n = i18n
Date.prototype.format = function (pattern) {
return format(this, pattern)
}
Date.of = parse
Date.parseOf = parse
})()
const nal = require('./nal')
console.log(1, Date.parseOf('>>2021-05-14 오전 11:43:14.341', ' yyyy-MM-dd a HH:mm:ss.SSS'))
console.log(2, Date.parseOf('20210514114314'))
console.log(3, Date.parseOf('20210514 114314', 'yyyyMMdd HHmmss'))
console.log(4, Date.parseOf('20210514'))
console.log(5, new Date().format('yyyy-MM-dd a hh:mm:ss.SSS'))
console.log(6, new Date().format('yy-M-d a h:m:s.S'))
console.log(7, new Date().format('yyyy년 MM월 dd일 a hh시 mm분 ss초(SSS)'))
console.log(8, new Date().format('yyyyMMddaHHmmssSSS'))
console.log(9, new Date().format())
@greenn-lab
Copy link
Author

update// use destructuring in method "parseOf"

@greenn-lab
Copy link
Author

update// unit test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment