最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

dart - How to add months to a JewishDate with kosher_dart - Stack Overflow

matteradmin4PV0评论

I'm using kosher_dart with JewishDate and JewishCalendar.

I want to add days, months and years to my JewishDate.

Adding days works Fine - I convert the JewishDate into DateTime, add days, and convert it back. Adding years also works – I just use jewishDate.setJewishDate(jewishYear + years, jewishMonth, jewishDayOfMonth).

But I'm stuck on adding months.

Since the Jewish calendar has both 12-month and 13-month years (leap years), simply adding months can result in invalid dates, especially if the target month doesn't exist in a non-leap year (e.g., Adar II).

I want to add months efficiently, in O(1) time – without using loops or checking year-by-year.

Is there a reliable way to calculate the resulting Jewish date after adding a number of months, while properly handling leap years and varying month lengths?

I'm using kosher_dart with JewishDate and JewishCalendar.

I want to add days, months and years to my JewishDate.

Adding days works Fine - I convert the JewishDate into DateTime, add days, and convert it back. Adding years also works – I just use jewishDate.setJewishDate(jewishYear + years, jewishMonth, jewishDayOfMonth).

But I'm stuck on adding months.

Since the Jewish calendar has both 12-month and 13-month years (leap years), simply adding months can result in invalid dates, especially if the target month doesn't exist in a non-leap year (e.g., Adar II).

I want to add months efficiently, in O(1) time – without using loops or checking year-by-year.

Is there a reliable way to calculate the resulting Jewish date after adding a number of months, while properly handling leap years and varying month lengths?

Share Improve this question asked 22 hours ago נתן אנטרנתן אנטר 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Being unfamiliar with the Jewish calendar, I apologize if I miss something, but it would seem like the intended way to offset a JewishDate is by using the forward method:

JewishDate date = JewishDate();
date.forward(Calendar.MONTH, 1);

Note that this edits the date in-place. If you want to get a new instance, you will have to use the clone method, then call forward on the clone:

JewishDate today = JewishDate();
JewishDate nextMonth = date.clone()..forward(Calendar.MONTH, 1);
Post a comment

comment list (0)

  1. No comments so far