javascript - 获取波斯月的最后一天 + 解决方案
问题描述
浏览网页后,我找不到任何功能来做到这一点,我有个人解决方案是可行的。也许它对某人有用。
**使用 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;
}
}
解决方案
你可以在下个月的第一天之前找到一天。例如,如果要查找第 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>
推荐阅读
- sql - 为什么我在选择语句中得到超过 1 行?
- mysql - Mysql ASC 如何命令 1,2,3,4,5 而不是 1,10,11,12?
- elasticsearch - Nest updatebyquery 如何使用 poco 而不是编写脚本
- javascript - Context2D 定义后未定义
- c# - 如何使用 Swashbuckle.AspNetCore 隐藏响应代码 200?
- c# - 正确解析 log4jxmlevent 渲染器的时间戳属性
- java - Streams,检查两个对象列表是否具有相同的另一个对象的嵌套列表
- php - 如何将 HTML 代码翻译成 wp_nav_menu Wordpress?
- javascript - 有什么办法可以简化这种方法吗?在 jquery 上隐藏/显示元素
- css - 使用 display flex 的复杂布局