mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
handling am/pm
This commit is contained in:
@@ -14,6 +14,5 @@ describe("Can properly handle expected date formats", () => {
|
|||||||
const dateString = "8/27/2024 23:95:00"
|
const dateString = "8/27/2024 23:95:00"
|
||||||
const dateObject = getDateFromString(dateString)
|
const dateObject = getDateFromString(dateString)
|
||||||
expect(dateObject).not.toBeUndefined()
|
expect(dateObject).not.toBeUndefined()
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -1,39 +1,52 @@
|
|||||||
|
|
||||||
|
const _getDateFromAMPM = (datePart: string, timePartWithMeridian: string): Date | undefined => {
|
||||||
export const getDateFromString = (value: string) => {
|
|
||||||
// Updated regex to match both formats: "MM/DD/YYYY HH:mm:ss" and "M/D/YYYY h:mm:ss AM/PM"
|
|
||||||
const validDateRegex = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}:\d{2}(?:\s?[APap][Mm])?$/;
|
|
||||||
if (!validDateRegex.test(value)) {
|
|
||||||
console.log("invalid date format", value);
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [datePart, timePartWithMeridian] = value.split(" ");
|
|
||||||
const [day, month, year] = datePart.split("/").map(Number);
|
const [day, month, year] = datePart.split("/").map(Number);
|
||||||
let [timePart, meridian] = timePartWithMeridian.split(" ");
|
const [timePart, meridian] = timePartWithMeridian.split(" ");
|
||||||
const [hours, minutes, seconds] = timePart.split(":").map(Number);
|
const [hours, minutes, seconds] = timePart.split(":").map(Number);
|
||||||
|
|
||||||
let adjustedHours = hours;
|
let adjustedHours = hours;
|
||||||
if (meridian) {
|
if (meridian) {
|
||||||
meridian = meridian.toUpperCase();
|
const upperMeridian = meridian.toUpperCase();
|
||||||
if (meridian === "PM" && hours < 12) {
|
if (upperMeridian === "PM" && hours < 12) {
|
||||||
adjustedHours += 12;
|
adjustedHours += 12;
|
||||||
} else if (meridian === "AM" && hours === 12) {
|
} else if (upperMeridian === "AM" && hours === 12) {
|
||||||
adjustedHours = 0;
|
adjustedHours = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const date = new Date(year, month - 1, day, adjustedHours, minutes, seconds);
|
const date = new Date(year, month - 1, day, adjustedHours, minutes, seconds);
|
||||||
|
return isNaN(date.getTime()) ? undefined : date;
|
||||||
|
};
|
||||||
|
|
||||||
if (isNaN(date.getTime())) {
|
const _getDateFromMilitary = (datePart: string, timePart: string): Date | undefined => {
|
||||||
console.log("could not parse time out of value", value);
|
const [day, month, year] = datePart.split("/").map(Number);
|
||||||
|
const [hours, minutes, seconds] = timePart.split(":").map(Number);
|
||||||
|
|
||||||
|
const date = new Date(year, month - 1, day, hours, minutes, seconds);
|
||||||
|
return isNaN(date.getTime()) ? undefined : date;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDateFromString = (value: string): Date | undefined => {
|
||||||
|
// Regex for AM/PM format: "M/D/YYYY h:mm:ss AM/PM"
|
||||||
|
const ampmDateRegex = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}:\d{2}\s{1}[APap][Mm]$/;
|
||||||
|
|
||||||
|
// Regex for military time format: "MM/DD/YYYY HH:mm:ss"
|
||||||
|
const militaryDateRegex = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}:\d{2}$/;
|
||||||
|
|
||||||
|
if (ampmDateRegex.test(value)) {
|
||||||
|
const [datePart, timePartWithMeridian] = value.split(/[\s\u202F]+/);
|
||||||
|
return _getDateFromAMPM(datePart, timePartWithMeridian);
|
||||||
|
} else if (militaryDateRegex.test(value)) {
|
||||||
|
const [datePart, timePart] = value.split(" ");
|
||||||
|
return _getDateFromMilitary(datePart, timePart);
|
||||||
|
} else {
|
||||||
|
console.log("invalid date format", value);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return date;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const verifyDateStringOrUndefined = (
|
export const verifyDateStringOrUndefined = (
|
||||||
value: string
|
value: string
|
||||||
): string | undefined => {
|
): string | undefined => {
|
||||||
|
|||||||
Reference in New Issue
Block a user