salat-first
Version:
Islamic prayer times calculation with special support for Moroccan methods and Maliki madhab
110 lines (109 loc) • 3.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addDays = addDays;
exports.addMinutes = addMinutes;
exports.addSeconds = addSeconds;
exports.roundToNearestMinute = roundToNearestMinute;
exports.dayOfYear = dayOfYear;
exports.isLeapYear = isLeapYear;
exports.hoursMinutesFromDecimal = hoursMinutesFromDecimal;
exports.formatTime = formatTime;
exports.formatTime12Hour = formatTime12Hour;
/**
* Adds a specified number of days to a date
* @param date The date to add days to
* @param days The number of days to add
* @returns A new date with the days added
*/
function addDays(date, days) {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
/**
* Adds a specified number of minutes to a date
* @param date The date to add minutes to
* @param minutes The number of minutes to add
* @returns A new date with the minutes added
*/
function addMinutes(date, minutes) {
return addSeconds(date, minutes * 60);
}
/**
* Adds a specified number of seconds to a date
* @param date The date to add seconds to
* @param seconds The number of seconds to add
* @returns A new date with the seconds added
*/
function addSeconds(date, seconds) {
return new Date(date.getTime() + seconds * 1000);
}
/**
* Rounds a date to the nearest minute
* @param date The date to round
* @returns A new date rounded to the nearest minute
*/
function roundToNearestMinute(date) {
const seconds = date.getSeconds();
let offset = seconds >= 30 ? 60 - seconds : -seconds;
return addSeconds(date, offset);
}
/**
* Gets the day of the year (1-366)
* @param date The date
* @returns The day of the year
*/
function dayOfYear(date) {
const start = new Date(date.getFullYear(), 0, 0);
const diff = date.getTime() - start.getTime();
const oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}
/**
* Checks if a year is a leap year
* @param year The year
* @returns Whether the year is a leap year
*/
function isLeapYear(year) {
if (year % 4 !== 0) {
return false;
}
if (year % 100 === 0 && year % 400 !== 0) {
return false;
}
return true;
}
/**
* Gets the hours and minutes from a decimal hour
* @param decimalHour The decimal hour (e.g., 12.5 for 12:30)
* @returns An object with hours and minutes
*/
function hoursMinutesFromDecimal(decimalHour) {
const hours = Math.floor(decimalHour);
const minutes = Math.floor((decimalHour - hours) * 60);
return { hours, minutes };
}
/**
* Formats a date in HH:MM format
* @param date The date to format
* @returns The formatted time string
*/
function formatTime(date) {
return date.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
}
/**
* Formats a date in 12-hour format with AM/PM
* @param date The date to format
* @returns The formatted time string
*/
function formatTime12Hour(date) {
return date.toLocaleTimeString([], {
hour: "numeric",
minute: "2-digit",
hour12: true,
});
}