Created
November 21, 2024 12:03
-
-
Save mityukov/f91cf48d521406b4a4c6c72341ab9769 to your computer and use it in GitHub Desktop.
Parse dates in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calcYear(yearRes, monthStr) | |
{ | |
const month = parseInt(monthStr); | |
const curMonth = (new Date).getMonth(); | |
const curYear = (new Date).getFullYear(); | |
yearStr = yearRes === undefined | |
? calcYear(monthStr) | |
: (yearRes.length === 4 ? yearRes : '20' + yearRes); | |
return curMonth > month ? '' + (curYear + 1) : '' + curYear; | |
} | |
function parseDate(dateStr) // return YYYY-MM-DD | |
{ | |
const monthNames = ['___', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; | |
// YYYY-MM-DD: | |
if (dateStr.match(/\d\d\d\d-\d\d-\d\d/)) { | |
return dateStr; | |
} | |
// MM/DD/YYYY or MM/DD/YY or MM/DD | |
let res = dateStr.match(/^(\d\d?)\/(\d\d?)(\/(\d\d\d?\d?))?$/); | |
if (res) { | |
const monthStr = res[1].padStart(2, '0'); | |
const dayStr = res[2].padStart(2, '0'); | |
const yearStr = calcYear(res[4], monthStr); | |
return `${yearStr}-${monthStr}-${dayStr}`; | |
} | |
// MM-DD-YYYY or MM-DD-YY or MM-DD | |
res = dateStr.match(/^(\d\d?)-(\d\d?)(-(\d\d\d?\d?))?$/); | |
if (res) { | |
const monthStr = res[1].padStart(2, '0'); | |
const dayStr = res[2].padStart(2, '0'); | |
const yearStr = calcYear(res[4], monthStr); | |
return `${yearStr}-${monthStr}-${dayStr}`; | |
} | |
// DD.MM.YYYY or DD.MM.YY or DD.MM | |
res = dateStr.match(/^(\d\d?)\.(\d\d?)(\.(\d\d\d?\d?))?$/); | |
if (res) { | |
const monthStr = res[2].padStart(2, '0'); | |
const dayStr = res[1].padStart(2, '0'); | |
const yearStr = calcYear(res[4], monthStr); | |
return `${yearStr}-${monthStr}-${dayStr}`; | |
} | |
// Nov 24, [20]24 or Nov 20 [20]24 | |
res = dateStr.match(/^(\w\w\w+)[, ]+(\d\d?)([, ]+(\d\d\d?\d?))?$/) | |
if (res) { | |
const monthIndex = monthNames.indexOf(res[1].trim().toLowerCase().substring(0, 3)); | |
if (monthIndex) { | |
const monthStr = String(monthIndex).padStart(2, '0'); | |
const dayStr = res[2].padStart(2, '0'); | |
const yearStr = calcYear(res[4], monthStr); | |
return `${yearStr}-${monthStr}-${dayStr}`; | |
} | |
} | |
// 24 Nov, [20]24 or 24 Nov [20]24 | |
res = dateStr.match(/^(\d\d?)[, ]+(\w\w\w+)([, ]+(\d\d\d?\d?))?$/) | |
if (res) { | |
const monthIndex = monthNames.indexOf(res[2].trim().toLowerCase().substring(0, 3)); | |
if (monthIndex) { | |
const monthStr = String(monthIndex).padStart(2, '0'); | |
const dayStr = res[1].padStart(2, '0'); | |
const yearStr = calcYear(res[4], monthStr); | |
return `${yearStr}-${monthStr}-${dayStr}`; | |
} | |
} | |
return ''; | |
} | |
// test it: | |
console.log({ | |
'2024-12-25': parseDate('2024-12-25'), | |
// MM/DD/YYYY or MM/DD/YY or MM/DD: | |
'12/25/2024': parseDate('12/25/2024'), | |
'12/25/24': parseDate('12/25/24'), | |
'12/25': parseDate('12/25'), | |
'01/07/2025': parseDate('01/07/2025'), | |
'1/7/2025': parseDate('1/7/2025'), | |
'1/7/25': parseDate('1/7/25'), | |
'1/7': parseDate('1/7'), | |
// MM-DD-YYYY or MM-DD-YY or MM-DD: | |
'12-25-2024': parseDate('12-25-2024'), | |
'12-25-24': parseDate('12-25-24'), | |
'12-25': parseDate('12-25'), | |
'01-07-2025': parseDate('01-07-2025'), | |
'1-7-2025': parseDate('1-7-2025'), | |
'1-7-25': parseDate('1-7-25'), | |
'1-7': parseDate('1-7'), | |
// DD.MM.YYYY or DD.MM.YY or DD.MM: | |
'25.12.2024': parseDate('25.12.2024'), | |
'25.12.24': parseDate('25.12.24'), | |
'25.12': parseDate('25.12'), | |
'07.01.2025': parseDate('07.01.2025'), | |
'07.01.25': parseDate('07.01.25'), | |
'7.1.2025': parseDate('7.1.2025'), | |
'7.1.25': parseDate('7.1.25'), | |
'7.1': parseDate('7.1'), | |
'Dec 25, 2024': parseDate('Dec 25, 2024'), | |
'dec 25 2024': parseDate('dec 25 2024'), | |
'DEC 25 24': parseDate('DEC 25 24'), | |
'December 25': parseDate('Dec 25'), | |
'Jan 07, 2025': parseDate('Jan 07, 2025'), | |
'january 07 2025': parseDate('jan 07 2025'), | |
'JAN 07 25': parseDate('JAN 07 25'), | |
'Jan 07': parseDate('Jan 07'), | |
'25 Dec, 2024': parseDate('25 Dec, 2024'), | |
'25, dec 2024': parseDate('25 dec 2024'), | |
'25 DEC 24': parseDate('25 DEC 24'), | |
'25 December': parseDate('25 December'), | |
'07 Jan, 2025': parseDate('07 Jan, 2025'), | |
'07 january 2025': parseDate('07 january 2025'), | |
'07 JAN 25': parseDate('07 JAN 25'), | |
'07, Jan': parseDate('07 Jan'), | |
'something invalid': parseDate('invalid') || defaultDate, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment