File size: 1,060 Bytes
cf86710 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import { format as dfFormat } from "date-fns";
import type { DateLibOptions } from "../../classes/DateLib.js";
/** Format override adding +543 to year tokens for Buddhist Era (BE). */
export function format(
date: Date,
formatStr: string,
options?: DateLibOptions,
): string {
const beYear = date.getFullYear() + 543;
switch (formatStr) {
case "LLLL y":
case "LLLL yyyy":
return `${dfFormat(date, "LLLL", options)} ${beYear}`;
case "LLLL":
return dfFormat(date, "LLLL", options);
case "yyyy":
return String(beYear).padStart(4, "0");
case "y":
return String(beYear);
case "yyyy-MM":
return `${beYear}-${dfFormat(date, "MM", options)}`;
case "yyyy-MM-dd":
return `${beYear}-${dfFormat(date, "MM", options)}-${dfFormat(date, "dd", options)}`;
case "PPP":
case "PPPP": {
const raw = dfFormat(date, formatStr, options);
return raw.replace(/(.*)(\d{4})(?!.*\d)/, (_m, pre) => `${pre}${beYear}`);
}
default:
return dfFormat(date, formatStr, options);
}
}
|