This repository has been archived on 2024-02-11. You can view files and clone it, but cannot push or open issues or pull requests.
sitio/helpers/date.js
Cat /dev/Nulo cc6091c7a0
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
oops
2023-11-11 23:37:38 -10:00

66 lines
1.7 KiB
JavaScript

const z = require("zod");
const dateishExp = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/;
const zDateish = z
.string()
.regex(dateishExp)
.transform((s) => {
const found = s.match(dateishExp);
return {
year: parseInt(found.groups.year),
month: parseInt(found.groups.month),
day: parseInt(found.groups.day),
};
});
/** @typedef {z.infer<zDateish>} Dateish */
const dateFormatter = new Intl.DateTimeFormat("es-AR", {
weekday: "long",
day: "numeric",
month: "long",
year: "numeric",
timeZone: "UTC",
});
/**
* @param {Dateish} dateish
* @returns Date
*/
function dateishToDate(dateish) {
return new Date(dateish.year, dateish.month - 1, dateish.day);
}
/**
* @param {Date} date
* @param {boolean} upper
* @returns string
*/
function formatDate(date, upper = false) {
const formatted = dateFormatter.format(date);
if (upper) {
// no le digan a la policía del unicode!
return formatted[0].toUpperCase() + formatted.slice(1);
} else return formatted;
}
/**
* @param {Dateish} dateis
* @returns string
*/
function dateishToString({ year, month, day }) {
return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
}
// /**
// * @param {Dateish} dateish
// * @param {{ itemprop?: string, upper?: boolean }} params
// * @returns string
// */
// function dateishToElement(dateish, { itemprop, upper } = {}) {
// const itempropParam = (itemprop && ` itemprop="${itemprop}"`) || "";
// return `<time datetime="${dateishToString(dateish)}" ${itempropParam}>${formatDate(
// dateishToDate(dateish),
// upper
// )}</time>`;
// }
module.exports = { formatDate, zDateish, dateishToDate };