# Search a date range for muhurta windows

> The API answers "what are the windows on this date". A booking flow asks "when in the next week can we do it". The search in between is a few lines of interval arithmetic that you own, because the rules are your product's.

Source: https://astronode.dev/recipes/muhurta-search-date-range · Updated: 2026-12-01

**What you build:** A list of bookable 60-minute-plus windows over seven days, from 14 calls (Node.js)

## Per-day building blocks

There is no range endpoint; each muhurta endpoint takes one `date` plus place. Two are enough for a common search:

- [`/v1/muhurta/choghadiya`](https://astronode.dev/capabilities/vedic/muhurta-choghadiya) - eight day and eight night windows, each with `name`, `lord` and `quality` (`auspicious`, `neutral` or `inauspicious`).
- [`/v1/muhurta/inauspicious`](https://astronode.dev/capabilities/vedic/muhurta-inauspicious) - Rahu Kaal, Yamaganda and Gulika for the day, to subtract.

[`/v1/muhurta/auspicious`](https://astronode.dev/capabilities/vedic/muhurta-auspicious) (Abhijit, Brahma Muhurat, Amrit Kaal) slots in the same way if your rule wants them. Which windows count as good is a decision for you and your users; the code makes it a constant.

## The search

Per day: fetch both, keep the choghadiya windows your rule accepts, subtract every blocked interval (the three kaals plus the hours the venue is closed), and keep what remains above a minimum length. Days are independent, so fetch a few concurrently:

muhurta-search.mjs

```js
// muhurta-search.mjs - node muhurta-search.mjs
const API = process.env.ASTRONODE_API_URL ?? "https://api.astronode.dev";
const KEY = process.env.ASTRONODE_API_KEY;

const PLACE = { tz_offset_minutes: 330, lat: 28.6139, lng: 77.209 }; // New Delhi
const ZONE = "Asia/Kolkata";
const GOOD = new Set(["Amrit", "Shubh", "Labh"]); // your product's rule, not the API's
const MIN_MINUTES = 60;
const OPEN = "09:00", CLOSE = "18:00"; // when the venue can actually do it

const post = async (path, body) => {
  const res = await fetch(API + path, {
    method: "POST",
    headers: { authorization: `Bearer ${KEY}`, "content-type": "application/json" },
    body: JSON.stringify(body),
  });
  if (!res.ok) throw new Error(`${path} ${res.status}`);
  return (await res.json()).data;
};

const hhmm = (ms) => new Intl.DateTimeFormat("en-GB", { timeZone: ZONE, hour: "2-digit", minute: "2-digit" }).format(ms);
const localMs = (date, t) => Date.parse(`${date}T${t}:00+05:30`);

// Subtract each blocked interval from [s, e); returns what is left.
const subtract = (s, e, blocked) =>
  blocked.reduce((parts, [bs, be]) => parts.flatMap(([ps, pe]) =>
    be <= ps || bs >= pe ? [[ps, pe]] : [[ps, Math.min(pe, bs)], [Math.max(ps, be), pe]].filter(([a, b]) => b > a)),
  [[s, e]]);

const searchDay = async (date) => {
  const body = { date, ...PLACE };
  const [chog, bad] = await Promise.all([post("/v1/muhurta/choghadiya", body), post("/v1/muhurta/inauspicious", body)]);
  const blocked = Object.values(bad).filter((w) => w?.start).map((w) => [Date.parse(w.start), Date.parse(w.end)]);
  blocked.push([0, localMs(date, OPEN)], [localMs(date, CLOSE), Infinity]);
  return chog.day
    .filter((w) => GOOD.has(w.name))
    .flatMap((w) => subtract(Date.parse(w.start), Date.parse(w.end), blocked).map(([s, e]) => ({ date, name: w.name, s, e })))
    .filter((w) => (w.e - w.s) / 60000 >= MIN_MINUTES);
};

const dates = Array.from({ length: 7 }, (_, i) => new Date(Date.UTC(2026, 11, 7 + i)).toISOString().slice(0, 10));
// 2 calls a day. Days are independent, so run them a few at a time, not all at once.
const found = [];
for (let i = 0; i < dates.length; i += 3) {
  for (const day of await Promise.all(dates.slice(i, i + 3).map(searchDay))) found.push(...day);
}
for (const w of found) console.log(`${w.date}  ${hhmm(w.s)}-${hhmm(w.e)}  ${w.name} (${Math.round((w.e - w.s) / 60000)} min)`);
console.log(`${found.length} windows from ${dates.length * 2} calls`);
```

Output (New Delhi, 7-13 Dec 2026):

```text
2026-12-07  09:36-10:54  Shubh (78 min)
2026-12-07  14:48-16:06  Labh (78 min)
2026-12-07  16:06-17:24  Amrit (78 min)
2026-12-08  10:55-12:12  Labh (78 min)
2026-12-09  16:06-17:24  Labh (78 min)
2026-12-10  12:13-13:31  Labh (78 min)
2026-12-10  16:07-17:24  Shubh (78 min)
2026-12-11  09:38-10:56  Amrit (78 min)
2026-12-11  12:14-13:31  Shubh (78 min)
2026-12-12  14:49-16:07  Amrit (78 min)
2026-12-13  09:40-10:57  Labh (78 min)
2026-12-13  10:57-12:15  Amrit (78 min)
2026-12-13  13:32-14:50  Shubh (78 min)
13 windows from 14 calls
```

Days are uneven: 8 and 9 December offer one window each, 13 December offers three - every other Amrit, Shubh or Labh slot on those days overlaps a kaal or falls outside 09:00-18:00. A range can also come back empty, which your UI needs a message for. Adjacent windows (7 Dec 14:48-17:24, 13 Dec 09:40-12:15) can be merged if your booking slots are longer than one choghadiya.

## Cost and caching

- **Calls = days x endpoints.** A week is 14 calls; a 90-day wedding-date search is 180. On the Hobby plan's per-minute limit, fetch in small batches as above rather than 180 at once - or plan it with the [rate-limit planner](https://astronode.dev/tools/rate-limit-planner).
- **Cache per place and date.** Windows depend only on date and location, so every user searching Delhi for the same week shares the 14 responses. Precompute popular cities nightly.
- **Night windows** are in `chog.night` and run past midnight UTC-wise; include them only if your product does evening events.
- **The UTC trap again:** `09:00` here is built as `+05:30` local. For DST zones derive the offset per date, as in [daily panchang for a city](https://astronode.dev/recipes/daily-panchang-for-a-city).

## Questions

**What do I end up with?**

A list of bookable 60-minute-plus windows over seven days, from 14 calls. The code is in Node.js.

**Which endpoints does this recipe call, and in what order?**

3 endpoints, in this order: Choghadiya (`POST /v1/muhurta/choghadiya`), then Rahu Kaal, Yamaganda and Gulika (`POST /v1/muhurta/inauspicious`), then Auspicious Muhurta (Abhijit, Amrit Kaal) (`POST /v1/muhurta/auspicious`).

## Endpoints, in call order

- [`POST /v1/muhurta/choghadiya`](https://astronode.dev/capabilities/vedic/muhurta-choghadiya) - Choghadiya API
- [`POST /v1/muhurta/inauspicious`](https://astronode.dev/capabilities/vedic/muhurta-inauspicious) - Rahu Kaal, Yamaganda and Gulika API
- [`POST /v1/muhurta/auspicious`](https://astronode.dev/capabilities/vedic/muhurta-auspicious) - Auspicious Muhurta (Abhijit, Amrit Kaal) API
