Skip to content

Commit

Permalink
non-standate date formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jparkerweb committed Nov 29, 2024
1 parent bf8dc6e commit a846242
Showing 1 changed file with 74 additions and 6 deletions.
80 changes: 74 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,46 @@ class RichFootPlugin extends Plugin {
let modifiedDate;
if (this.settings.customModifiedDateProp && frontmatter && frontmatter[this.settings.customModifiedDateProp]) {
modifiedDate = frontmatter[this.settings.customModifiedDateProp];
if (!isNaN(Date.parse(modifiedDate))) {
let isValidDate = false;
let tempDate = modifiedDate;

// Try original string
if (!isNaN(Date.parse(tempDate))) {
isValidDate = true;
}
// Try replacing periods with hyphens (only first two occurrences)
if (!isValidDate) {
let count = 0;
tempDate = modifiedDate.replace(/\./g, (match) => {
count++;
return count <= 2 ? '-' : match;
});
if (!isNaN(Date.parse(tempDate))) {
isValidDate = true;
}
}
// Try replacing forward slashes with hyphens (only first two occurrences)
if (!isValidDate) {
let count = 0;
tempDate = modifiedDate.replace(/\//g, (match) => {
count++;
return count <= 2 ? '-' : match;
});
if (!isNaN(Date.parse(tempDate))) {
isValidDate = true;
}
}

if (isValidDate) {
// Split on 'T' to handle timestamps, then split the date part
const datePart = modifiedDate.split('T')[0];
const [year, month, day] = datePart.split('-').map(Number);
const datePart = tempDate.split('T')[0];
// Handle different separators
const parts = datePart.split(/[-./]/);
const [year, month, day] = parts.map(Number);
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
modifiedDate = `${months[month - 1]} ${day}, ${year}`;
} else {
modifiedDate = modifiedDate;
}
} else {
modifiedDate = new Date(file.stat.mtime);
Expand All @@ -412,12 +446,46 @@ class RichFootPlugin extends Plugin {
let createdDate;
if (this.settings.customCreatedDateProp && frontmatter && frontmatter[this.settings.customCreatedDateProp]) {
createdDate = frontmatter[this.settings.customCreatedDateProp];
if (!isNaN(Date.parse(createdDate))) {
let isValidDate = false;
let tempDate = createdDate;

// Try original string
if (!isNaN(Date.parse(tempDate))) {
isValidDate = true;
}
// Try replacing periods with hyphens (only first two occurrences)
if (!isValidDate) {
let count = 0;
tempDate = createdDate.replace(/\./g, (match) => {
count++;
return count <= 2 ? '-' : match;
});
if (!isNaN(Date.parse(tempDate))) {
isValidDate = true;
}
}
// Try replacing forward slashes with hyphens (only first two occurrences)
if (!isValidDate) {
let count = 0;
tempDate = createdDate.replace(/\//g, (match) => {
count++;
return count <= 2 ? '-' : match;
});
if (!isNaN(Date.parse(tempDate))) {
isValidDate = true;
}
}

if (isValidDate) {
// Split on 'T' to handle timestamps, then split the date part
const datePart = createdDate.split('T')[0];
const [year, month, day] = datePart.split('-').map(Number);
const datePart = tempDate.split('T')[0];
// Handle different separators
const parts = datePart.split(/[-./]/);
const [year, month, day] = parts.map(Number);
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
createdDate = `${months[month - 1]} ${day}, ${year}`;
} else {
createdDate = createdDate;
}
} else {
createdDate = new Date(file.stat.ctime);
Expand Down

0 comments on commit a846242

Please sign in to comment.