Home > Software engineering >  Get Hebrew calendar date with JS
Get Hebrew calendar date with JS

Time:12-14

I am trying to get the Hebrew calendar date so I can base a function off of the current Hebrew date. Something like this

if (monthName == "Tishrei") {
    $('.Tishrei').css('display', 'block')

so I can control what shows up on my webpage based on what today's date is.

I tried to get the number of the month like this

var monthNumber = new Intl.DateTimeFormat('en-u-ca-hebrew', { month: 'narrow' }).format(new Date());

which correctly returns the number of the month but when I tried to use it in a switch statement it didn't work, it just returned the default. Here is my switch statement:

switch (monthNumber) {
      case 1:
        monthName = "Tishrei"
        break;

I also thought about using the names of the months -


var monthNumber = new Intl.DateTimeFormat('en-u-ca-hebrew', { month: 'narrow' }).format(new Date());

but the spelling of Hebrew words written in English varies and I don't know which spelling is used in this case.

Any ideas?

CodePudding user response:

So after console logging - typeof(monthNumber) it shows us that it is a string.

You need to change the monthNumber to a number.

Here is how you can do that:

var monthNumber = Number(new Intl.DateTimeFormat('en-u-ca-hebrew', { month: 'narrow' }).format(new Date()))

switch (monthNumber) {
  case 1:
    monthName = "Tishrei"
  case 2:
    monthName = "Cheshvan"
  case 3:
    monthName = "Kislev"
  break
}

console.log(monthName)
  • Related