Skip to content

Instantly share code, notes, and snippets.

@mjradwin
Last active April 24, 2023 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjradwin/0b7c4f2a33acfc30cf8d6235c173187f to your computer and use it in GitHub Desktop.
Save mjradwin/0b7c4f2a33acfc30cf8d6235c173187f to your computer and use it in GitHub Desktop.
Displaying today's omer count
<div id="hebcal-omer"></div>
<script defer>
function pad2(number) {
if (number < 10) {
return '0' + number;
}
return '' + number;
}
let dt = new Date();
if (dt.getHours() > 19) {
dt.setDate(dt.getDate() + 1);
}
const isoDate = [dt.getFullYear(), pad2(dt.getMonth() + 1), pad2(dt.getDate())].join('-');
const url = `https://www.hebcal.com/hebcal?v=1&cfg=json&o=on&lg=s&start=${isoDate}&end=${isoDate}`;
fetch(url, {method: 'GET', credentials: 'omit'})
.then(response => response.json())
.then(data => {
if (data.items && data.items.length) {
for (const item of data.items) {
if (item.category === 'omer') {
console.log(item);
// You might also choose to display item.omer.count.en or item.omer.sefira.he
document.getElementById('hebcal-omer').innerText = item.title;
}
}
}
});
</script>
@mjradwin
Copy link
Author

If you want different language (for example Hebrew), change &lg=s to &lg=h

@ShulAdmin
Copy link

let me start by thanking yall so much for making the amazing api available!!! Chazak u'Baruch

the example code is problematic because it uses ISO dates which changes sooner than our local because of timezones.

I instead wrote this code to yield a true to local time count with a daily text of
Sunday night is 18 days, which is 2 weeks and 4 days of the Omer


   function getYyyyMmDd(dt) {
      return dt.toLocaleString('en-us', {year: 'numeric', month: '2-digit', day: '2-digit'}).replace(/(\d+)\/(\d+)\/(\d+)/, '$3-$1-$2');
    }

    async function updateZmannimTickerText() {
      let todayDate = new Date();
      const today = todayDate.toLocaleDateString('en-us', { weekday: "long" });

      let tomorrowDate = new Date();
      tomorrowDate.setDate(todayDate.getDate() + 1);


      const todayIsoDate = getYyyyMmDd(todayDate);
      const tomorrowIsoDate = getYyyyMmDd(tomorrowDate);
      const url = `https://www.hebcal.com/hebcal?v=1&cfg=json&o=on&lg=s&start=${tomorrowIsoDate}&end=${tomorrowIsoDate}`;
      const resp = await fetch(url, { method: 'GET', credentials: 'omit' });
      const respJson = await resp.json();
      console.log(respJson);
      let omerTxt = '';
      if (respJson.items && respJson.items.length) {
        for (const item of respJson.items) {
          if (item.category === 'omer') {
            omerTxt = `${item.omer.count.en}`.replace(`Today`, `${today} night`);
          }
        }
      }


      zmannimTickerText = `${omerTxt}`;
      document.querySelector(`.zmannim-box`).innerText = zmannimTickerText;
    }

@mjradwin
Copy link
Author

Thanks for the feedback. We've updated the gist to use the local date/time object instead of the toISOString() function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment