Skip to content

Commit

Permalink
fix: date format
Browse files Browse the repository at this point in the history
  • Loading branch information
majusss committed Sep 4, 2024
1 parent c24f050 commit c8cab44
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/lib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,21 @@ const months: Record<string, string> = {
};

export const convertTextDate = (inputDate: string): string => {
const regexes = [
/(\d{1,2}\.?) (\w+) (\d{4})/,
/(\d{1,2})\.(\d{1,2})\.(\d{4})/,
];
if (regexes[0].test(inputDate)) {
const matchResult = inputDate.match(regexes[0]);
if (matchResult) {
const [, day, month, year] = matchResult;
return `${year}-${
months[month.toLowerCase().padStart(2, "0")]
}-${day.padStart(2, "0").replace(".", "")}`;
}
} else if (regexes[1].test(inputDate)) {
const matchResult = inputDate.match(regexes[1]);
if (matchResult) {
const [, day, month, year] = matchResult;
return `${year}-${month}-${day}`;
inputDate = inputDate.replace(/r\./, "").trim();

for (const [monthName, monthNumber] of Object.entries(months)) {
if (inputDate.includes(monthName)) {
const [day, year] = inputDate.replace(monthName, "").trim().split(/\s+/);
return `${year}-${monthNumber}-${day.padStart(2, "0")}`;
}
}
// const dateParts = inputDate.split(" ");
// if(dateParts.length === 3 && dateParts[1].length > 2) {}

const regex = /(\d{1,2})\.(\d{1,2})\.(\d{4})/;
const matchResult = inputDate.match(regex);
if (matchResult) {
const [, day, month, year] = matchResult;
return `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`;
}

return inputDate;
};

0 comments on commit c8cab44

Please sign in to comment.