@churchapps/helpers
Version:
Library of helper functions not specific to any one ChurchApps project or framework.
173 lines • 6.21 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateHelper = void 0;
const date_fns_1 = require("date-fns");
const dayjs_1 = __importDefault(require("dayjs"));
// Use dynamic import for ES module compatibility with dayjs plugins
const loadUtcPlugin = async () => {
const utc = await Promise.resolve().then(() => __importStar(require("dayjs/plugin/utc")));
dayjs_1.default.extend(utc.default || utc);
};
// Initialize the plugin
loadUtcPlugin().catch(() => {
console.warn("Could not load dayjs UTC plugin");
});
class DateHelper {
//Fixes timezone issues when you just need the date.
static toDate(input) {
return new Date(Date.parse(input.toString().replace("Z", "")));
}
static toDateTime(input) {
return new Date(Date.parse(input.toString()));
}
//obsolete. Do not use
static convertToDate(input) {
return this.toDateTime(input);
}
static addDays(date, days) {
const result = new Date(date.getTime());
result.setDate(result.getDate() + days);
return result;
}
static prettyDate(date) {
if (date === undefined || date === null)
return "";
return this.formatDateTime(date, "MMM d, yyyy");
}
static prettyDateTime(date) {
if (date === undefined || date === null)
return "";
return this.formatDateTime(date, "MMM d, yyyy h:mm a");
}
static prettyTime(date) {
if (date === undefined || date === null)
return "";
return this.formatDateTime(date, "h:mm a");
}
static getLastSunday() {
let result = new Date();
while (result.getDay() !== 0)
result.setDate(result.getDate() - 1);
return result;
}
static getNextSunday() {
let result = this.getLastSunday();
result.setDate(result.getDate() + 7);
return result;
}
static getWeekSunday(year, week) {
let result = new Date(year, 0, 1);
while (result.getDay() !== 0)
result.setDate(result.getDate() + 1);
result.setDate(result.getDate() + ((week - 1) * 7));
return result;
}
static formatHtml5Date(date) {
let result = "";
if (date !== undefined && date !== null) {
try {
result = new Date(date).toISOString().split("T")[0];
}
catch { }
}
return result;
}
static formatHtml5Time(time) {
let h = time.getHours();
let m = time.getMinutes();
let s = time.getSeconds();
return `${h < 10 ? ("0" + h) : h}:${m < 10 ? ("0" + m) : m}:${s < 10 ? ("0" + s) : s}`;
}
static formatHtml5DateTime(date) {
if (date === undefined || date === null)
return "";
else {
return this.formatDateTime(date, "yyyy-MM-dd") + "T" + this.formatDateTime(date, "HH:mm");
}
}
static getDisplayDuration(d) {
let seconds = Math.round((new Date().getTime() - d.getTime()) / 1000);
if (seconds > 86400) {
let days = Math.floor(seconds / 86400);
return (days === 1) ? "1d" : days.toString() + "d";
}
else if (seconds > 3600) {
let hours = Math.floor(seconds / 3600);
return (hours === 1) ? "1h" : hours.toString() + "h";
}
else if (seconds > 60) {
let minutes = Math.floor(seconds / 60);
return (minutes === 1) ? "1m" : minutes.toString() + "m";
}
else
return (seconds === 1) ? "1s" : Math.floor(seconds).toString() + "s";
}
static getShortDate(d) {
return (d.getMonth() + 1).toString() + "/" + (d.getDate()).toString() + "/" + d.getFullYear().toString();
}
static convertDatePickerFormat(d) {
const date = this.formatHtml5Date(d).split("-");
if (date.length === 3)
return new Date(`${date[1]}-${date[2]}-${date[0]}`);
return new Date();
}
static formatDateTime(date, format) {
try {
return (0, date_fns_1.format)(date, format);
}
catch {
return "";
}
}
static toMysqlDate(d) {
if (d === null || d === undefined) {
return undefined;
}
return (0, dayjs_1.default)(d).format("YYYY-MM-DD HH:mm:ss");
}
static subtractHoursFromNow(hour) {
const now = new Date();
return new Date(now.setHours(now.getHours() - hour));
}
static toUTCDate(d) {
return (0, dayjs_1.default)(d).utc().format("YYYY-MM-DD HH:mm:ss");
}
}
exports.DateHelper = DateHelper;
//# sourceMappingURL=DateHelper.js.map