Skip to content

Instantly share code, notes, and snippets.

@cognitom
Created January 30, 2017 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cognitom/56d73f23e7671b40368f396cef47ddba to your computer and use it in GitHub Desktop.
Save cognitom/56d73f23e7671b40368f396cef47ddba to your computer and use it in GitHub Desktop.
openBDの日付フィールドのノーマライズ
function normalizeDate (raw) {
const patterns = [
// 2017-01-30
{re: /^\d{4}-\d{2}-\d{2}($|,)/, f: m => m[0]},
// 2017-01
{re: /^\d{4}-\d{2}($|,)/, f: m => m[0]},
// 2017
{re: /^\d{4}($|,)/, f: m => m[0]},
// 20170130
{re: /^(\d{4})(\d{2})(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}-${m[3]}`},
// 201701
{re: /^(\d{4})(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}`},
// c2017-01
{re: /^c(\d{4})-(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}`},
// [2017]-01
{re: /^\[(\d{4})\]-(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}`}
]
for (const pattern of patterns) {
const m = raw.match(pattern.re)
if (m) return pattern.f(m)
}
return 'unrecognized'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment