Saju (Korean Four Pillars) and BaZi charts are built from four pillars: year, month, day and hour. Each pillar is a pair of characters, one Heavenly Stem and one Earthly Branch. The year and month pillars depend on the sun's position, which takes real astronomy. The day and hour pillars do not. They are pure calendar arithmetic.
This post shows the arithmetic in JavaScript, then the one edge case that causes most disagreements between apps: births between 23:00 and midnight. We checked the code against the sxtwl calendar library on 3,000 random dates and hours from 1900 to 2100, with zero mismatches.
The 60-day cycle
There are 10 Heavenly Stems and 12 Earthly Branches:
js
export const STEMS = '甲乙丙丁戊己庚辛壬癸';
export const BRANCHES = '子丑寅卯辰巳午未申酉戌亥';
export const pillar = (i) => STEMS[i % 10] + BRANCHES[i % 12];
Stems and branches advance together, one step per day. Because 10 and 12 share a factor of 2, they line up again after 60 days, not 120. So there are exactly 60 pairs, from 甲子 (index 0) to 癸亥 (index 59). pillar(i) turns an index into its pair, since index i always lands on stem i % 10 and branch i % 12.
The day count has been kept without a break for well over two thousand years. That is what makes it computable: every day since then has a fixed position in the cycle.
Day pillar from a Julian Day Number
A Julian Day Number (JDN) is a plain count of days. Astronomers use it so that date arithmetic never has to deal with months. The standard conversion from a Gregorian date:
js
export function jdn(y, m, d) {
const a = Math.floor((14 - m) / 12);
const yy = y + 4800 - a;
const mm = m + 12 * a - 3;
return d + Math.floor((153 * mm + 2) / 5) + 365 * yy
+ Math.floor(yy / 4) - Math.floor(yy / 100) + Math.floor(yy / 400) - 32045;
}
Since the day cycle and the JDN both advance by one per day, the day pillar is the JDN plus a fixed offset, mod 60. The offset is 49:
js
export function dayIndex(y, m, d) {
return (jdn(y, m, d) + 49) % 60;
}
pillar(dayIndex(2000, 1, 1)); // '戊午'
You can check the offset against any known day. January 1, 2000 has JDN 2451545 and is a 戊午 day, index 54. (2451545 + 49) mod 60 is 54.
One rule matters more than the formula: use the local calendar date at the place of birth. Do not convert a New York birth to Korean time first. Moving the date by one day changes the day pillar, and the day pillar holds the Day Master, the center of the whole chart.
Hour branch
The day is split into 12 two-hour blocks. The block for 子 (Rat) runs from 23:00 to 00:59, 丑 from 01:00 to 02:59, and so on:
js
export function hourBranch(hour) {
return Math.floor((hour + 1) / 2) % 12;
}
At hour 23, (23 + 1) / 2 is 12, and 12 mod 12 is 0, which is 子. At hour 0 it is 0 as well. At hour 14 it is 7, which is 未, the block from 13:00 to 14:59.
In a real app, apply true solar time first, so the hour you pass in is the sun's hour at the birthplace and not the clock's. Our post on true solar time covers that conversion in JavaScript.
Hour stem
The hour stem is not free. It depends on the day stem, through a rule Korean and Chinese texts teach as a short verse (五鼠遁). On a 甲 or 己 day, the 子 hour is 甲子. On an 乙 or 庚 day it is 丙子. Each pair of day stems shifts the start by two stems. In code that is one line:
js
export function hourPillar(dayStem, branch) {
const stem = (dayStem * 2 + branch) % 10;
return STEMS[stem] + BRANCHES[branch];
}
For a 甲 day, dayStem is 0 and the 子 hour gets stem 0, 甲. For an 乙 day, dayStem is 1 and the 子 hour gets stem 2, 丙. The verse and the formula agree for all ten.
The 23:00 problem
Here is the part that makes two apps give the same person two different charts.
The 子 hour starts at 23:00, an hour before midnight. In the common schools, the 子 hour is the first hour of a new day, so its stem comes from the next day's stem. They disagree about the day pillar:
The day changes at 23:00. A birth at 23:30 gets the next day's pillar.
The day changes at midnight. A birth at 23:30 keeps today's day pillar. Only the hour pillar moves to the next day. In Korean this late hour is called 야자시 (夜子時).
Both are in real use. An app should pick one on purpose and say which, or let the user choose:
js
// lateRat: 'sameDay' keeps today's day pillar (야자시)
// 'nextDay' moves the day pillar to tomorrow
export function dayAndHour({ year, month, day, hour, lateRat = 'sameDay' }) {
const today = dayIndex(year, month, day);
const next = (today + 1) % 60;
const late = hour >= 23;
const dayIdx = late && lateRat === 'nextDay' ? next : today;
const stemDay = late ? next : today;
const b = hourBranch(hour);
return { day: pillar(dayIdx), hour: hourPillar(stemDay % 10, b) };
}
dayAndHour({ year: 1990, month: 5, day: 15, hour: 14 });
// { day: '庚辰', hour: '癸未' }
dayAndHour({ year: 1990, month: 5, day: 15, hour: 23 });
// { day: '庚辰', hour: '戊子' }
dayAndHour({ year: 1990, month: 5, day: 15, hour: 23, lateRat: 'nextDay' });
// { day: '辛巳', hour: '戊子' }
Notice that the hour pillar is 戊子 in both cases. Only the day pillar differs, and with it the Day Master: 庚 Metal in one chart, 辛 Metal in the other.
Our first version got this wrong in a subtle way. It took the 23:00 hour stem from the current day. That matches neither school. The test against sxtwl caught it at once: every mismatch was at hour 23. If you write your own, test the 23:00 hour on purpose.
How we tested it
The test is short. Generate random dates and hours, compute the pillars, and compare with an independent implementation:
js
for (let i = 0; i < 3000; i++) {
const t = Date.UTC(1900, 0, 1) + Math.floor(Math.random() * 200 * 365.25) * 86400000;
const d = new Date(t);
const hour = Math.floor(Math.random() * 24);
const r = dayAndHour({
year: d.getUTCFullYear(), month: d.getUTCMonth() + 1, day: d.getUTCDate(), hour,
});
// write year, month, day, hour, r.day, r.hour to a CSV
}
We compared the CSV with Python's sxtwl (getDayGZ() and getHourGZ(hour)), which follows the 야자시 convention by default. All 3,000 rows matched.
What this does not cover
Year and month pillars. They change at solar terms, which depend on the sun's longitude. They need an astronomical formula, not a day count.
Interpretation. These functions build the chart. Reading it is another matter.
If you want to see a full chart built from these pillars, with the year and month pillars and the 10-year Luck Cycles added, the free Saju reading at Saju Lucky runs it in the browser.
Top comments (0)