Skip to content

Wrong result of interval.getTotalMonths  #33

@hackwaly

Description

@hackwaly

Consider interval of 2021-06-26, 2022-06-25. interval.getTotalMonths returns -1 instead of 11.

Here's my code fix it outside of datetime package.

@:access(datetime)
class IntervalFix {
	public static function getTotalMonthsFix(interval:DateTimeIntervalCore) {
		final begin = interval.begin;
		final end = interval.end;
		var months = (end.getYear() - begin.getYear()) * 12 + (end.getMonth() - begin.getMonth());
		var d1 = begin.getDay();
		var d2 = end.getDay();
		if (d2 < d1) {
			months --;
		} else if (d1 == d2) {
			var h1 = begin.getHour();
			var h2 = end.getHour();
			if (h2 < h1) {
				months --;
			} else if (h2 == h1) {
				var m1 = begin.getMinute();
				var m2 = end.getMinute();
				if (m2 < m1) {
					months --;
				} else if (m2 == m1 && end.getSecond() < begin.getSecond()) {
					months --;
				}
			}
		}
		return months;
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions