波斯日期时间的日期公式,计算闰年,周等

时间:2016-01-28 15:47:23

标签: javascript calendar date-arithmetic persian

旧标题:如何计算给定月份的星期数

由于我提供的答案,我改变了标题......

我正在创建一个库,然后将其包装在我的项目中,这会在我的文化中提供一些日期信息,

图书馆是这样的:

///<reference path="IDate.ts"/>
/**
 * Created by Hassan on 1/28/2016.
 */
// Help References:
// 1- http://www.aftabir.com/encyclopedia/urgent/calendar/chronometry/leapyear_new.php
var PersianDate = (function () {
    function PersianDate(year, month, day, firstDayOfWeek) {
        this.day = day;
        this.month = month;
        this.year = year;
        this.firstDayOfWeek = firstDayOfWeek;
    }
    PersianDate.prototype.isLeapYear = function () {
        var year = this.year;
        //var a = 0.025;
        //var b = 266;
        //var leapDays0;
        //var leapDays1;
        //var frac0;
        //var frac1;
        //if (year > 0) {
        //    leapDays0 = ((year + 38) % 2820) * 0.24219 + a; // 0.24219 ~ extra days of one year
        //    leapDays1 = ((year + 39) % 2820) * 0.24219 + a; // 38 days is the difference of epoch to 2820-year cycle
        //}
        //else if (year < 0) {
        //    leapDays0 = ((year + 39) % 2820) * 0.24219 + a;
        //    leapDays1 = ((year + 40) % 2820) * 0.24219 + a;
        //}
        //else {
        //    return false;
        //}
        //frac0 = Math.floor((leapDays0 - Math.floor(leapDays0)) * 1000);
        //frac1 = Math.floor((leapDays1 - Math.floor(leapDays1)) * 1000);
        //if (frac0 <= b && frac1 > b)
        //    return true;
        //else
        //    return false;

        var a = year+2346;
        var a2 = year-1 + 2346; //(A-1)=N-1+2346 -> 'A-1' is just a name of last year
        var b1 = Math.ceil(a * 365.24219879);
        var b2 = Math.ceil(a2 * 365.24219879);

        return b1 - b2 == 366;
    };
    PersianDate.prototype.getDaysInMonth = function () {
        switch (this.month) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                return 31;
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
                return 30;
            case 12:
                return (this.isLeapYear() ? 30 : 29);
        }
        return undefined;
    };
    PersianDate.prototype.getDaysInWeek = function () {
        return 7;
    };
    PersianDate.prototype.getDaysInYear = function () {
        return 365 + (this.isLeapYear() ? 1 : 0);
    };
    PersianDate.prototype.getMonthsInYear = function () {
        return 12;
    };
    PersianDate.prototype.getWeeksInYear = function () {
        return this.getDaysInYear() + (this.getWeekFirstDay()) / 7;
    };
    PersianDate.prototype.getWeeksInMonth = function () {
        var month = this.getMonth();
        console.log(month);
        var totalDaysToMonthStart;
        if (month <= 7) {
            totalDaysToMonthStart = (month - 1) * 31;
            console.log(totalDaysToMonthStart)
        }
        else {
            totalDaysToMonthStart = 186 + ((month - 6 - 1) * 30);
            console.log(totalDaysToMonthStart)
        }
        var remainedDayWithoutStartingDays = 7 - (totalDaysToMonthStart% 7);

        var WeeksAfterStarting = Math.floor((this.getDaysInMonth() - remainedDayWithoutStartingDays) / 7);

        console.log("remainedDayWithoutStartingDays: ", remainedDayWithoutStartingDays);
        console.log("WeeksAfterStarting: ", WeeksAfterStarting)
        return WeeksAfterStarting + (remainedDayWithoutStartingDays != 0)? 1:0;
    };
    PersianDate.prototype.getWeekFirstDay = function () {
        var n = this.year;
        var a = n - 1;
        var b = a + 2346;
        var yearLength = b * 365.24219879; //tool sal motevasete khorshidi
        var c = Math.ceil(yearLength);
        //نظر به آنکه مبداء سال ۲۳۴۶- روز سه‌شنبه بوده است (و با انتساب اعداد صفر و يک تا شش به شنبه و يکشنبه تا جمعه) به C سه واحد مى‌افزائيم يا از آن چهار واحد کم مى‌کنيم تا به مبداء شنبه انتقال يابيم و عدد حاصل را d مى‌ناميم.
        var d = c + 3;
        var r = d % 7;
        console.log("First day of year: " + r);
        return r;
    };
    PersianDate.prototype.getDay = function () {
        return this.day;
    };
    PersianDate.prototype.getWeekOfYear = function () {
        console.log("Week of Year: " + this.getDayOfYear() + this.getWeekFirstDay() / 7);
        return this.getDayOfYear() + this.getWeekFirstDay() / 7;
    };
    PersianDate.prototype.getWeekOfMonth = function () {
        return undefined;
    };
    PersianDate.prototype.getDayOfWeek = function () {
        return undefined;
    };
    PersianDate.prototype.getDayOfMonth = function () {
        return undefined;
    };
    PersianDate.prototype.getDayOfYear = function () {
        return undefined;
    };
    PersianDate.prototype.getMonth = function () {
        return this.month;
    };
    PersianDate.prototype.getYear = function () {
        return this.year;
    };
    return PersianDate;
})();
//# sourceMappingURL=PersianDate.js.map

var pd = new PersianDate(1395, 1, 1);
pd.getWeekFirstDay();
pd.getWeeksInMonth();

一周的第一天返回0到6(我不确定标准日期时间是否这样做)

并且问题在于getWeeksInMonth:

首先我让它发挥作用,但是在第一个星期完整的日子里,它失败了,然后我改变它,现在它根本不起作用。 我搜查了,我无法得到正确的公式。 (更好的说我根本没有任何公式)

我应该怎样做才能在一个月内返回几周:

给定日期 的 1395至1301年 它应该像图像: enter image description here

正如你在第五个月和第一个月看到的那样,我们在一个月内有6个星期,其他的是5天。 如何解决它? 我在各种日期都有几个星期的问题:&#39;(

谢谢。

注意:对于将来的任何人都可以访问此主题,此类基于PersianCalendar,而不是GregorianCalendar。

1 个答案:

答案 0 :(得分:0)

自从有人帮助我解决这个问题以来,确实需要很多时间,然后我忘了这一切,但我做了一个存储库,所以重要的公式就在那里:

https://github.com/deadmann/Calendar