This commit is contained in:
2025-11-09 20:21:46 +01:00
parent bfab2f2cdc
commit 20cf597c5c
15 changed files with 101 additions and 142 deletions
+57
View File
@@ -0,0 +1,57 @@
const ensureDate = (value) => {
if (!value) {
return null;
}
const date = value instanceof Date ? new Date(value.getTime()) : new Date(value);
return Number.isNaN(date.getTime()) ? null : date;
};
export const formatDate = (value, { fallback = '—', locale, options } = {}) => {
const date = ensureDate(value);
if (!date) {
return fallback;
}
return date.toLocaleDateString(locale, options);
};
export const formatDateTime = (value, { fallback = '—', locale, options } = {}) => {
const date = ensureDate(value);
if (!date) {
return fallback;
}
return date.toLocaleString(locale, options);
};
export const toDateInputValue = (value) => {
const date = ensureDate(value);
if (!date) {
return '';
}
const timezoneOffset = date.getTimezoneOffset();
const localDate = new Date(date.getTime() - timezoneOffset * 60000);
return localDate.toISOString().slice(0, 10);
};
export const toIssuedTimestamp = (dateString, fallback) => {
if (!dateString) {
return null;
}
const base = ensureDate(fallback) || new Date();
const [year, month, day] = dateString.split('-').map((part) => Number.parseInt(part, 10));
if (!year || !month || !day) {
return null;
}
const candidate = new Date(base);
candidate.setUTCFullYear(year, month - 1, day);
return Number.isNaN(candidate.getTime()) ? null : candidate.toISOString();
};
export const parseDateValue = (value) => ensureDate(value);
export default {
formatDate,
formatDateTime,
toDateInputValue,
toIssuedTimestamp,
parseDateValue,
};
+13
View File
@@ -0,0 +1,13 @@
export const clamp = (value, min, max) => {
if (value < min) {
return min;
}
if (value > max) {
return max;
}
return value;
};
export default {
clamp,
};