Utility Functions
Date and Time Transformation
/**
* Convert ISO-8601 string (UTC) to MySQL DATETIME format.
* Example: "2025-09-16T14:20:45Z" -> "2025-09-16 14:20:45"
*/
export function isoToMySQL(isoString) {
const date = new Date(isoString);
if (isNaN(date.getTime())) {
throw new Error("Invalid ISO-8601 string");
}
return date.toISOString().slice(0, 19).replace("T", " ");
}
/**
* Convert MySQL DATETIME string to ISO-8601 (UTC).
* Example: "2025-09-16 14:20:45" -> "2025-09-16T14:20:45Z"
*/
export function mysqlToISO(mysqlDateTime) {
const date = new Date(mysqlDateTime + "Z"); // force UTC
if (isNaN(date.getTime())) {
throw new Error("Invalid MySQL DATETIME string");
}
return date.toISOString();
}
Last updated