javascript - Date formula, Calculating Leap Year, Week, etc, in Persian Datetime -
old title: how calculate number of week in given month
due answer provide change title...
i'm creating library wrap in project, provide date information in culture,
the library this:
///<reference path="idate.ts"/> /** * created hassan on 1/28/2016. */ // 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 = 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 ~ days of 1 year // leapdays1 = ((year + 39) % 2820) * 0.24219 + a; // 38 days difference of epoch 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 = year+2346; var a2 = year-1 + 2346; //(a-1)=n-1+2346 -> 'a-1' 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 = n - 1; var b = + 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();
first day of week return 0 6 (i'm not sure if standard datetime or not)
and thing become problematic getweeksinmonth:
first made work, in month full set of days in first weeks failed, change it, doesn't work @ all. searched , couldn't right formula. (better didn't got formula @ all)
what should return weeks in given month:
for given date 1395/01 should image:
as can see in fifth , eleven month have 6 week in 1 month, , other 5days. hopw fix it? kinda have issue weeks in kinda dates :'(
thank you.
note: guys in future may visit thread, class based on persiancalendar, not gregoriancalendar.
it sure take lot time since helped me solve issue, , forgot all, made repository, important formula there:
Comments
Post a Comment