首页 > 解决方案 > 获取波斯月的最后一天 + 解决方案

问题描述

浏览网页后,我找不到任何功能来做到这一点,我有个人解决方案是可行的。也许它对某人有用。

**使用 Moment 插件转换日期。***moment(currentPersianDate).clone().endof('month').format('YYYY/MM/DD') => 不适用于波斯日期

  function GetLastDayOfPersianMonth(calendar) {
            const currentDate = new Date();
            const currentPersianDate = moment(currentDate).locale('fa').format('YYYY/M/D');
            const splitDate = currentPersianDate.split("/");
            const year = splitDate[0];
            const month = splitDate[1];
            const lastDayOfPersianMonth = CalculateLeapYear(month, year);
    
            const endPersianMonth = year + "/" + month + "/" + lastDayOfPersianMonth;
        };

    function CalculateLeapYear(month, year) {
            if (month <= 6) {
                return 31;
            }
            if (month > 6 && month < 12) {
                return 30;
            }

            const leapMatch = [1, 5, 9, 13, 17, 22, 26, 30];
            const number = year % 33;
            const isLeap = leapMatch.includes(number);
           
            if (month == 12 && !isLeap) {
                return 29;
            }
            if (month == 12 && isLeap) {
                return 30;
            }
        }

标签: javascriptmomentjs

解决方案


你可以在下个月的第一天之前找到一天。例如,如果要查找第 6 个月的最后一天:

const firstDayOfNextMonth = moment.from('1400/07/01', 'fa', 'YYYY/MM/DD');
const lastDayOfMonth = firstDayOfNextMonth.add(-1,'d');
const lastDayOfMonthPersianDate = lastDayOfMonth.locale('fa').format('YYYY/M/D');

所以对于当月的最后一天:

const currentDate = new Date();
const currentPersianDate = moment(currentDate).locale('fa').add(1,'month');
const firstDayOfNextMonthSt = currentPersianDate.format('jYYYY') + '/' + currentPersianDate.format('jM') + '/01';
//and then
const firstDayOfNextMonth = moment.from(firstDayOfNextMonthSt, 'fa', 'YYYY/MM/DD');
const lastDayOfMonth = firstDayOfNextMonth.add(-1,'d');
const lastDayOfMonthPersianDate = lastDayOfMonth.locale('fa').format('YYYY/M/D');

您可以编写这样的函数并使用它:

function getLastDayOfMonthPersianDate(firstDayOfNextMonthSt){
    const firstDayOfNextMonth = moment.from(firstDayOfNextMonthSt, 'fa', 'YYYY/MM/DD');
    const lastDayOfMonth = firstDayOfNextMonth.add(-1,'d');
    const lastDayOfMonthPersianDate = lastDayOfMonth.locale('fa').format('YYYY/M/D');

    return lastDayOfMonthPersianDate;
}

查看函数用法的片段:

function getLastDayOfMonthPersianDate(firstDayOfNextMonthSt){
    const firstDayOfNextMonth = moment.from(firstDayOfNextMonthSt, 'fa', 'YYYY/MM/DD');
    const lastDayOfMonth = firstDayOfNextMonth.add(-1,'d');
    const lastDayOfMonthPersianDate = lastDayOfMonth.locale('fa').format('YYYY/M/D');
    
    return lastDayOfMonthPersianDate;
}

const lastDayOfMonthPersianDate = getLastDayOfMonthPersianDate('1400/07/01');
console.log('last day of the 6th month');
console.log(lastDayOfMonthPersianDate);
console.log('-------');

const currentDate = new Date();
const currentPersianDate = moment(currentDate).locale('fa').add(1,'month');
const firstDayOfNextMonthSt = currentPersianDate.format('jYYYY') + '/' + currentPersianDate.format('jM') + '/01';
const lastDayOfCurrentMonthPersianDate = getLastDayOfMonthPersianDate(firstDayOfNextMonthSt);
console.log('last day of the current month');
console.log(lastDayOfCurrentMonthPersianDate);
<script src="https://unpkg.com/jalali-moment/dist/jalali-moment.browser.js"></script>


推荐阅读