@bxshefby/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
152 lines (150 loc) • 5.77 kB
JavaScript
/**
* @package @bxshefby/b24jssdk
* @version 3.3.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bx-shef/b24-js-sdk
* @see https://b24-js-sdk.bx-shef.by/
*/
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const TOKEN_NAMES = [
"MMMMM",
"LLLLL",
"EEEEE",
"ccccc",
"yyyy",
"MMMM",
"LLLL",
"EEEE",
"cccc",
"MMM",
"LLL",
"EEE",
"ccc",
"SSS",
"ooo",
"uuu",
"ZZZ",
"yy",
"MM",
"LL",
"dd",
"HH",
"hh",
"mm",
"ss",
"qq",
"uu",
"ZZ",
"y",
"M",
"L",
"d",
"H",
"h",
"m",
"s",
"S",
"a",
"E",
"c",
"q",
"o",
"u",
"x",
"X",
"Z"
];
function formatDate(date, format) {
if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
return "Invalid DateTime";
}
const pad = /* @__PURE__ */ __name((value, length = 2) => String(Math.trunc(Math.abs(value))).padStart(length, "0"), "pad");
const name = /* @__PURE__ */ __name((options) => new Intl.DateTimeFormat("en-US", options).format(date), "name");
const offset = /* @__PURE__ */ __name((style) => {
const minutes = -date.getTimezoneOffset();
const sign = minutes < 0 ? "-" : "+";
const hours = Math.trunc(Math.abs(minutes) / 60);
const rest = Math.abs(minutes) % 60;
if (style === "short") {
return rest ? `${sign}${hours}:${pad(rest)}` : `${sign}${hours}`;
}
return style === "colon" ? `${sign}${pad(hours)}:${pad(rest)}` : `${sign}${pad(hours)}${pad(rest)}`;
}, "offset");
const hours12 = date.getHours() % 12 || 12;
const dayOfYear = Math.round((Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 1)) / 864e5) + 1;
const tokens = {
yyyy: /* @__PURE__ */ __name(() => pad(date.getFullYear(), 4), "yyyy"),
yy: /* @__PURE__ */ __name(() => pad(date.getFullYear() % 100), "yy"),
y: /* @__PURE__ */ __name(() => String(date.getFullYear()), "y"),
MMMMM: /* @__PURE__ */ __name(() => name({ month: "narrow" }), "MMMMM"),
LLLLL: /* @__PURE__ */ __name(() => name({ month: "narrow" }), "LLLLL"),
EEEEE: /* @__PURE__ */ __name(() => name({ weekday: "narrow" }), "EEEEE"),
ccccc: /* @__PURE__ */ __name(() => name({ weekday: "narrow" }), "ccccc"),
MMMM: /* @__PURE__ */ __name(() => name({ month: "long" }), "MMMM"),
MMM: /* @__PURE__ */ __name(() => name({ month: "short" }), "MMM"),
MM: /* @__PURE__ */ __name(() => pad(date.getMonth() + 1), "MM"),
M: /* @__PURE__ */ __name(() => String(date.getMonth() + 1), "M"),
LLLL: /* @__PURE__ */ __name(() => name({ month: "long" }), "LLLL"),
LLL: /* @__PURE__ */ __name(() => name({ month: "short" }), "LLL"),
LL: /* @__PURE__ */ __name(() => pad(date.getMonth() + 1), "LL"),
L: /* @__PURE__ */ __name(() => String(date.getMonth() + 1), "L"),
dd: /* @__PURE__ */ __name(() => pad(date.getDate()), "dd"),
d: /* @__PURE__ */ __name(() => String(date.getDate()), "d"),
EEEE: /* @__PURE__ */ __name(() => name({ weekday: "long" }), "EEEE"),
EEE: /* @__PURE__ */ __name(() => name({ weekday: "short" }), "EEE"),
cccc: /* @__PURE__ */ __name(() => name({ weekday: "long" }), "cccc"),
ccc: /* @__PURE__ */ __name(() => name({ weekday: "short" }), "ccc"),
HH: /* @__PURE__ */ __name(() => pad(date.getHours()), "HH"),
H: /* @__PURE__ */ __name(() => String(date.getHours()), "H"),
hh: /* @__PURE__ */ __name(() => pad(hours12), "hh"),
h: /* @__PURE__ */ __name(() => String(hours12), "h"),
mm: /* @__PURE__ */ __name(() => pad(date.getMinutes()), "mm"),
m: /* @__PURE__ */ __name(() => String(date.getMinutes()), "m"),
ss: /* @__PURE__ */ __name(() => pad(date.getSeconds()), "ss"),
s: /* @__PURE__ */ __name(() => String(date.getSeconds()), "s"),
SSS: /* @__PURE__ */ __name(() => pad(date.getMilliseconds(), 3), "SSS"),
S: /* @__PURE__ */ __name(() => String(date.getMilliseconds()), "S"),
a: /* @__PURE__ */ __name(() => date.getHours() < 12 ? "AM" : "PM", "a"),
E: /* @__PURE__ */ __name(() => String(date.getDay() || 7), "E"),
c: /* @__PURE__ */ __name(() => String(date.getDay() || 7), "c"),
qq: /* @__PURE__ */ __name(() => pad(Math.floor(date.getMonth() / 3) + 1), "qq"),
q: /* @__PURE__ */ __name(() => String(Math.floor(date.getMonth() / 3) + 1), "q"),
ooo: /* @__PURE__ */ __name(() => pad(dayOfYear, 3), "ooo"),
o: /* @__PURE__ */ __name(() => String(dayOfYear), "o"),
uuu: /* @__PURE__ */ __name(() => String(Math.floor(date.getMilliseconds() / 100)), "uuu"),
uu: /* @__PURE__ */ __name(() => pad(Math.floor(date.getMilliseconds() / 10)), "uu"),
u: /* @__PURE__ */ __name(() => pad(date.getMilliseconds(), 3), "u"),
x: /* @__PURE__ */ __name(() => String(date.getTime()), "x"),
X: /* @__PURE__ */ __name(() => String(Math.floor(date.getTime() / 1e3)), "X"),
ZZZ: /* @__PURE__ */ __name(() => offset("plain"), "ZZZ"),
ZZ: /* @__PURE__ */ __name(() => offset("colon"), "ZZ"),
Z: /* @__PURE__ */ __name(() => offset("short"), "Z")
};
let out = "";
for (let i = 0; i < format.length; ) {
if (format[i] === "'") {
const end = format.indexOf("'", i + 1);
if (end === i + 1) {
out += "'";
i += 2;
continue;
}
out += end === -1 ? format.slice(i + 1) : format.slice(i + 1, end);
i = end === -1 ? format.length : end + 1;
continue;
}
const token = TOKEN_NAMES.find((candidate) => format.startsWith(candidate, i));
if (token) {
out += tokens[token]();
i += token.length;
continue;
}
out += format[i];
i++;
}
return out;
}
__name(formatDate, "formatDate");
export { TOKEN_NAMES, formatDate };