UEA-Prodem
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
import { toGregorianDate, toHijriDate } from "../utils/conversion.js";
|
||||
import { getDaysInMonth } from "../utils/daysInMonth.js";
|
||||
export function setMonth(date, monthIndex) {
|
||||
const hijri = toHijriDate(date);
|
||||
// Handle overflow/underflow of monthIndex
|
||||
// Note: monthIndex argument is absolute month index for the year.
|
||||
// E.g. setMonth(..., 13) sets to Safar next year.
|
||||
let targetYear = hijri.year;
|
||||
let targetMonth = monthIndex;
|
||||
if (targetMonth > 11 || targetMonth < 0) {
|
||||
targetYear += Math.floor(targetMonth / 12);
|
||||
targetMonth = targetMonth % 12;
|
||||
if (targetMonth < 0) {
|
||||
targetMonth += 12;
|
||||
}
|
||||
}
|
||||
const daysInTargetMonth = getDaysInMonth(targetYear, targetMonth);
|
||||
const day = Math.min(hijri.day, daysInTargetMonth);
|
||||
return toGregorianDate({
|
||||
year: targetYear,
|
||||
monthIndex: targetMonth,
|
||||
day,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user