Date helpers
It's worth noting that for future projects, developers might consider using the Temporal API, which is a new date and time API in JavaScript. Temporal is designed to address many of the inconsistencies and limitations of the existing Date API. By leveraging Temporal, you can achieve more robust and reliable handling of date and time operations, such as local and ISO date-time conversions, without needing custom helper functions like toLocalISODateTime and toLocalISODate. This approach can enhance code readability, maintainability, and reduce the likelihood of common pitfalls associated with date manipulation in JavaScript.
toLocalISODateTime
Returns the local date as an ISO date string (Format yyyy-MM-ddTHH:mm:ss.SSS).
import { toLocalISODateTime } from "@olenbetong/appframe-core";
let dateStr = "2021-01-01T00:00:00.000";
let date = new Date(dateStr);
// This will fail
console.assert(dateStr === date.toISOString());
// This will succeed
console.assert(dateStr === toLocalISODateTime(date));toLocalISODate
Same as toLocalISODateTime, but returns only the date part (Format yyyy-MM-dd)
import { toLocalISODateTime } from "@olenbetong/appframe-core";
let dateStr = "2021-01-01T00:00:00.000";
let date = new Date(dateStr);
// This will fail
console.assert("2021-01-01" === date.toISOString());
// This will succeed
console.assert("2021-01-01" === toLocalISODate(date));toLocalDateTime
Converts a Date to a string compatible with <input type="datetime-local">. The UTC value of the date is treated as local time, and null or undefined returns an empty string.
Last updated
Was this helpful?