3

I have a web project where I need a function either server-side using php or client-side using javascript where I can pass in a Georgian date and have month(s) or years added to it according to the Hebrew calendar, and return a Julian date.

for example the date 12/8/2014 which is 15 Kislev when adding a month it should return 1/7/2014 which is the Hebrew date of 15 Tevet instead of 1/8/2014 which is returned by regular date add functions which is actually 16 Tevet.

I've searched around and couldn't find anything, Hebcal doesn't seem to have any of these params.

effy
  • 131
  • 5
  • 1
    Welcome to Mi Yodeya! If you're building a Jewish calendar, you might want to take a look at these related questions: http://judaism.stackexchange.com/q/37308/5323 http://judaism.stackexchange.com/q/39053/5323 – MTL Dec 08 '14 at 01:23
  • 1
    Also http://judaism.stackexchange.com/q/45363/5323 – MTL Dec 08 '14 at 01:29
  • What do you mean by "have month(s) or years added to it according to the hebrew calendar". Can you add examples to your question? – yydl Dec 08 '14 at 02:33
  • 1
    This question appears to be off-topic because it is about programming – ertert3terte Dec 08 '14 at 02:35
  • You have the start of a good Software Recommendations question here. Before we can migrate it, thought, you'll need to add some more details to be in line with our quality guidelines. What programming language should it be written in? How do you want to interface with it? What do you especially want it to do, and what would you not want? Add these details to your question and we would accept it on Software Recommendations. Thanks! –  Dec 08 '14 at 04:03
  • 1
    @yydl I updated the question, I hope the examples explain it better -Thanks – effy Dec 08 '14 at 05:24
  • Do you really mean Julian and not Gregorian? The west hasn't used the Julian calendar since the 16th century. Wiki explains the differences and history: http://en.wikipedia.org/wiki/Gregorian_calendar – Popular Isn't Right Dec 10 '14 at 17:04
  • @Bachrach44 Yep you're right I haven't worked much with dates, I'll update the question. Thanks – effy Dec 10 '14 at 17:41
  • Would you consider this tool to be helpful ? –  Sep 28 '18 at 00:10

2 Answers2

4

Check out my hebcal-js library, the perfect thing you need for this. Include it, and then you can use one of the following snippets:

This is only if you will never have dates in Elul (due to a bug):

var hebDate = new Hebcal.HDate(new Date(2014, 11 /* meaning 12 */, 8));
hebDate.setMonth(hebDate.getMonth() + 1);
var gregDate = hebDate.greg(); // a javascript Date object

This will work for any date:

var hebDate = new Hebcal.HDate(new Date(2014, 11 /* meaning 12 */, 8));
var gregDate = hebDate.getMonthObject().next().getDay(hebDate.getDate()).greg(); // a javascript Date object
Scimonster
  • 23,124
  • 4
  • 55
  • 127
  • Nice library. But don't you still need to account for wrapping years like I did in my answer? – yydl Dec 08 '14 at 06:39
  • Nope! Comes built in. – Scimonster Dec 08 '14 at 06:45
  • So how does the library know if I want Tishri of this year or the next? – yydl Dec 08 '14 at 06:54
  • It goes by Nisan as the first month, actually, so i'll assume you mean that. A month of 1 is Nisan this year, a month of 13 is Nisan next year. 25 is two years from now, etc. – Scimonster Dec 08 '14 at 06:57
  • My code also assumes that Nisan == 1. The problem is that the year doesn't start at 1, it starts at 7. So if we take (y/m/d) 5773/6/1 and add 1 we get 5773/7/1. Which is not correct! It should be 5774/7/1. Does your code handle this? – yydl Dec 08 '14 at 07:03
  • It does now using the second snippet i put up. I also just opened #15 related to this. – Scimonster Dec 08 '14 at 07:47
  • @Scimonster So if i would enter a date like 1/1/2014 and add a year the result returned would be 1/1/2015 assuming 2014 is a leap year? – effy Dec 08 '14 at 17:27
  • @effy I guess so. – Scimonster Dec 08 '14 at 17:52
1

Looking at your example, you can simply take advantage of the date conversion functions built into whichever library you are using. So if you want to add a Hebrew month to a Julian date, you can:

  1. Convert the Julian date into a Hebrew date (i.e. Hebrew Year / Month / Day)
  2. Take the Hebrew month value and add 1. Just keep in mind that if you are in Elul and add 1, you end up in Tishrei of the next year (the year wraps). Also, only allow it to go to Adar Sheini if it is a Hebrew leap year (once again, your library should be able to tell you if it is)
  3. Now use a function to convert this new Hebrew date (made up of y/m/d) back into Julian

Here's some pseudo-ish code (not tested):

function addAHebrewMonth(julianDate):
    hebrew_year, hebrew_month, hebrew_day = julianToHebrew(julianDate)
    hebrew_month += 1
    if hebrew_month == 7: //Tishrei wrap
        hebrew_year += 1

    //Adar II (this will depend on how your library deals with leap years...)
    if hebrew_month == 13 and not isHebrewLeapYear(hebrew_year):
        hebrew_month = 1 //wrap back to Nisan

    return hebrewToJulian(hebrew_year, hebrew_month, hebrew_day)

Voila!

yydl
  • 38,600
  • 6
  • 88
  • 285
  • Of course, because converting Hebrew and Julian dates are so simple. – Scimonster Dec 08 '14 at 06:27
  • @Scimonster Is that sarcasm I detect? – yydl Dec 08 '14 at 06:40
  • Yes it is. ;) I know how much code it was to do that (my answer). – Scimonster Dec 08 '14 at 06:45
  • @Scimonster My answer is assuming he has such functions (his question seemed to be emphasizing the need to calculate time differences as opposed to the actual conversion. I therefore assumed he already had such functions). – yydl Dec 08 '14 at 06:59