datezone
Version:
A lightweight and comprehensive date and timeZone utility library for JavaScript.
29 lines • 877 B
JavaScript
import { getCachedFormatter } from "./cache.js";
// Pre-allocated, strictly typed options
export const FULL_TS = {
day: "2-digit",
hour: "2-digit",
hour12: false,
millisecond: "3-digit",
minute: "2-digit",
month: "2-digit",
second: "2-digit",
year: "numeric",
};
export function formatToParts(date, timeZone, opts) {
const formatter = getCachedFormatter(timeZone, opts);
const parts = formatter.formatToParts(date);
const result = {};
// Handle standard parts that INTL supports
for (const part of parts) {
if (part.type in opts) {
result[part.type] = Number(part.value);
}
}
// Handle milliseconds separately since INTL doesn't support it
if ("millisecond" in opts) {
result["millisecond"] = date % 1000;
}
return result;
}
//# sourceMappingURL=format-parts.pub.js.map