### user You are a code expert. Please use your professional knowledge to generate accurate and professional responses. Make sure the code you provide is executable when possible. Write a Four Pillars (BaZi) charting software that only displays the Four Pillars and Decennial Cycles. ### assistant · thinking The user wants a BaZi (Four Pillars) charting software that displays only the Four Pillars and Decennial Cycles (大运). They want executable code. Let me write a self-contained HTML/JavaScript app — that's the easiest to run. Key technical requirements: 1. Convert Gregorian date/time to BaZi (Year, Month, Day, Hour pillars). 2. Year pillar: based on 立春 (Start of Spring) boundary, not Jan 1. 3. Month pillar: based on solar terms (節氣) — 12 major terms (節). 4. Day pillar: continuous 60-day cycle from a known reference. 5. Hour pillar: based on 時辰 (2-hour blocks) with 子時 handling. 6. Decennial cycles (大運): direction based on gender + year stem yin/yang; start from month pillar, count forward/backward; start age computed from days to next/previous solar term (3 days = 1 year). For accurate solar terms, I need an astronomical calculation. Let me implement a reasonably accurate solar longitude calculation using VSOP87-lite / simplified algorithm, or use a table-based approach. The most practical accurate approach: compute solar apparent longitude using a simplified but decent algorithm (Meeus), and find the time when solar longitude = 315° (立春), 330° (雨水)... etc. Let me implement: **Sun's apparent longitude (Meeus, low accuracy ~0.01°)** ``` T = (JD - 2451545.0)/36525 L0 = 280.46646 + 36000.76983*T + 0.0003032*T² M = 357.52911 + 35999.05029*T - 0.0001537*T² C = (1.914602 - 0.004817T - 0.000014T²) sin M + (0.019993 - 0.000101T) sin 2M + 0.000289 sin 3M true longitude = L0 + C apparent = true - 0.00569 - 0.00478 sin(Ω), Ω = 125.04 - 1934.136T ``` This gives accuracy about 0.01° which is ~15 minutes of time. Good enough for most purposes but borderline for solar term boundaries. Actually 0.01° / 0.9856°/day ≈ 0.01 day ≈ 15 minutes. Hmm, could be off by 15 min. For a charting tool that's acceptable-ish. Let me add more terms to improve. Actually, let me use a better approach: compute using higher precision. I could add the planetary perturbation terms (Meeus Chapter 25 higher accuracy uses VSOP87). Alternatively, use the "Astronomical Algorithms" simple version but with more periodic terms. Actually a good compromise: use the VSOP87D truncated series for Earth's heliocentric longitude. That's a lot of coefficients though. Let me instead use Meeus's approach with the full set of nutation/aberration and a moderately truncated VSOP87. Hmm. Alternative: Use the well-known algorithm from "Astronomical Algorithms" 2nd ed., chapter 25, which gives solar longitude to ~0.01°. Actually the low-accuracy method is 0.01°. Let me improve by using the higher-precision VSOP87 truncation for Earth L, B, R with the main terms. The main terms: L0 (Earth heliocentric longitude): 175347046, 0, 0 3341656, 4.6692568, 6283.07585 34894, 4.6261, 12566.1517 3497, 2.7441, 5753.3849 3418, 2.8289, 3.5231 3136, 3.6277, 77713.7715 2676, 4.4181, 7860.4194 2343, 6.1352, 3930.2097 1324, 0.7425, 11506.7698 1273, 2.0371, 529.691 1199, 1.1096, 1577.3435 990, 5.233, 5884.927 902, 2.045, 26.298 857, 3.508, 398.149 780, 1.179, 5223.694 753, 2.533, 5507.553 505, 4.583, 18849.228 492, 4.205, 775.523 357, 2.92, 0.067 317, 5.849, 11790.629 284, 1.899, 796.298 271, 0.315, 10977.079 243, 0.345, 5486.778 206, 4.806, 2544.314 205, 1.869, 5573.143 202, 2.458, 6069.777 156, 0.833, 213.299 132, 3.411, 2942.463 126, 1.083, 20.775 115, 0.645, 0.98 103, 0.636, 4694.003 102, 0.976, 15720.839 102, 4.267, 7.114 99, 6.21, 2146.17 98, 0.68, 155.42 86, 5.98, 161000.69 85, 1.3, 6275.96 85, 3.67, 71430.7 80, 1.81, 17260.15 79, 3.04, 12036.46 75, 1.76, 5088.63 74, 3.5, 3154.69 74, 4.68, 801.82 70, 0.83, 9437.76 62, 3.98, 8827.39 61, 1.82, 7084.9 57, 2.78, 6286.6 56, 4.39, 14143.5 56, 3.47, 6279.55 52, 0.19, 12139.55 52, 1.33, 1748.02 51, 0.28, 5856.48 49, 0.49, 1194.45 41, 5.37, 8429.24 41, 2.4, 19651.05 39, 6.17, 10447.39 37, 6.04, 10213.29 37, 2.57, 1059.38 36, 1.71, 2352.87 36, 1.78, 6812.77 33, 0.59, 17789.85 30, 0.44, 83996.85 30, 2.74, 1349.87 25, 3.16, 4690.48 L1: 628331966747, 0, 0 206059, 2.678235, 6283.07585 4303, 2.6351, 12566.1517 425, 1.59, 3.523 119, 5.796, 26.298 109, 2.966, 1577.344 93, 2.59, 18849.23 72, 1.14, 529.69 68, 1.87, 398.15 67, 4.41, 5507.55 59, 2.89, 5223.69 56, 2.17, 155.42 45, 0.4, 796.3 36, 0.47, 775.52 29, 2.65, 7.11 21, 5.34, 0.98 19, 1.85, 5486.78 19, 4.97, 213.3 17, 2.99, 6275.96 16, 0.03, 2544.31 16, 1.43, 2146.17 15, 1.21, 10977.08 12, 2.83, 1748.02 12, 3.26, 5088.63 12, 5.27, 1194.45 12, 2.08, 4694 11, 0.77, 553.57 10, 1.3, 6286.6 10, 4.24, 1349.87 9, 2.7, 242.73 9, 5.64, 951.72 8, 5.3, 2352.87 6, 2.65, 9437.76 6, 4.67, 4690.48 L2: 52919, 0, 0 8720, 1.0721, 6283.0758 309, 0.867, 12566.152 27, 0.05, 3.52 16, 5.19, 26.3 16, 3.68, 155.42 10, 0.76, 18849.23 9, 2.06, 77713.77 7, 0.83, 775.52 5, 4.66, 1577.34 4, 1.03, 7.11 4, 3.44, 5573.14 3, 5.14, 796.3 3, 6.05, 5507.55 3, 1.19, 242.73 3, 6.12, 529.69 3, 0.31, 398.15 3, 2.28, 553.57 2, 4.38, 5223.69 2, 3.75, 0.98 L3: 289, 5.844, 6283.076 35, 0, 0 17, 5.49, 12566.15 3, 5.2, 155.42 1, 4.72, 3.52 1, 5.3, 18849.23 1, 5.97, 242.73 L4: 114, 3.142, 0 8, 4.13, 6283.08 1, 3.84, 12566.15 L5: 1, 3.14, 0 R0: 100013989, 0, 0 1670700, 3.0984635, 6283.07585 13956, 3.05525, 12566.1517 3084, 5.1985, 77713.7715 1628, 1.1739, 5753.3849 1576, 2.8469, 7860.4194 925, 5.453, 11506.77 542, 4.564, 3930.21 472, 3.661, 5884.927 346, 0.964, 5507.553 329, 5.9, 5223.694 307, 0.299, 5573.143 243, 4.273, 11790.629 212, 5.847, 1577.344 186, 5.022, 10977.079 175, 3.012, 18849.228 110, 5.055, 5486.778 98, 0.89, 6069.78 86, 5.69, 15720.84 86, 1.27, 161000.69 65, 0.27, 17260.15 63, 0.92, 529.69 57, 2.01, 83996.85 56, 5.24, 71430.7 49, 3.25, 2544.31 47, 2.58, 775.52 45, 5.54, 9437.76 43, 6.01, 6275.96 39, 5.36, 4694 38, 2.39, 8827.39 37, 0.83, 19651.05 37, 4.9, 12139.55 36, 1.67, 12036.46 35, 1.84, 2942.46 33, 0.24, 7084.9 32, 0.18, 5088.63 32, 1.78, 398.15 28, 1.21, 6286.6 28, 1.9, 6279.55 26, 4.59, 10447.39 R1: 103019, 1.10749, 6283.07585 1721, 1.0644, 12566.1517 702, 3.142, 0 32, 1.02, 18849.23 31, 2.84, 5507.55 25, 1.32, 5223.69 18, 1.42, 1577.34 10, 5.91, 10977.08 9, 1.42, 6275.96 9, 0.27, 5486.78 R2: 4359, 5.7846, 6283.0758 124, 5.579, 12566.152 12, 3.14, 0 9, 3.63, 77713.77 6, 1.87, 5573.14 3, 5.47, 18849.23 R3: 145, 4.273, 6283.076 7, 3.92, 12566.15 R4: 4, 2.56, 6283.08 That's a lot of coefficients but gives high accuracy (~0.001" ... well, actually it's accurate to about 0.0001 AU). This is the standard Meeus VSOP87 truncated. Good. Then: - L = Earth heliocentric longitude, convert to geocentric sun longitude: Θ = L + 180° - β = -B (sun's latitude, small) - Convert to FK5: λ' = Θ - 1.397°T - 0.00031°T² ... actually Meeus 25.9: λ' = Θ - 1.397T - 0.00031T² ΔΘ = -0.09033" ... Actually: Θ (FK5) = Θ - 0.09033" Δβ = +0.03916"(cos λ' - sin λ') - Nutation and aberration: apparent λ = Θ + Δψ + aberration aberration = -20.4898"/R - Δψ from nutation. For our purposes, precision to ~0.0001° is way beyond needed. Even 0.01° is fine. But let's do it properly-ish. Then, solar term times: solve for λ = target. Use Newton iteration / bisection on JD. Now the BaZi logic. **Year Pillar**: The year changes at 立春 (solar longitude 315°). Find the 立春 that precedes or equals the birth time. Year stem/branch = based on that year. Year 4 CE (or 1984 = 甲子). Standard: For a given year Y (Gregorian), 甲子 year... The 立春 of 1984 begins 甲子 year. So: yearIndex = (solarYear - 1984) mod 60, where solarYear is the year of the 立春. Actually more precisely: if birth is before 立春 of the Gregorian year, then the BaZi year is previous year. **Month Pillar**: The month changes at the 12 "節" (major solar terms): 立春(315°), 驚蟄(345°), 清明(15°), 立夏(45°), 芒種(75°), 小暑(105°), 立秋(135°), 白露(165°), 寒露(195°), 立冬(225°), 大雪(255°), 小寒(285°). Wait — the month order in BaZi: - 寅月 starts at 立春 (315°) - 卯月 starts at 驚蟄 (345°) - 辰月 starts at 清明 (15°) - 巳月 starts at 立夏 (45°) - 午月 starts at 芒種 (75°) - 未月 starts at 小暑 (105°) - 申月 starts at 立秋 (135°) - 酉月 starts at 白露 (165°) - 戌月 starts at 寒露 (195°) - 亥月 starts at 立冬 (225°) - 子月 starts at 大雪 (255°) - 丑月 starts at 小寒 (285°) Month stem: determined by year stem. 五虎遁: - 甲/己 year → 寅月 is 丙寅 - 乙/庚 year → 寅月 is 戊寅 - 丙/辛 year → 寅月 is 庚寅 - 丁/壬 year → 寅月 is 壬寅 - 戊/癸 year → 寅月 is 甲寅 So monthStemIndex = (yearStemIndex % 5) * 2 + 2 + monthOffset, mod 10, where monthOffset = 0 for 寅月. Check: yearStem 甲(0): (0%5)*2+2 = 2 = 丙. ✓ 乙(1): 1*2+2 = 4 = 戊 ✓ 丙(2): 2*2+2 = 6 = 庚 ✓ 丁(3): 3*2+2=8 = 壬 ✓ 戊(4): 4*2+2=10 mod 10 = 0 = 甲 ✓ 己(5): 5%5=0 → 2 = 丙 ✓ Good. **Day Pillar**: Continuous 60-day cycle. Reference: 1949-10-01 is 甲子日? Let me use a reliable reference. A common reference: JDN. The day pillar can be computed from Julian Day Number. Known: 2000-01-01 (Gregorian) was 戊午日? Let me verify with a formula. Standard formula: For Julian Day Number (at noon), the day stem index = (JDN + 9) mod 10, day branch index = (JDN + 1) mod 12? Let me check. Let me use a known anchor: 1984-02-02 was 甲子日? Hmm, I need a solid anchor. Well-known: 1949年10月1日 is 甲子日. Let me check that. Actually I recall 1949-10-01 is 甲子日 — yes, that's a commonly cited fact (开国大典 甲子日). Hmm, some sources say it's 甲子日. Let me verify with another known: 2000-01-01 is 戊午日? Let's compute: from 1949-10-01 to 2000-01-01. Days: 1949-10-01 → 2000-10-01 is 51 years. Leap days between: 1952,56,60,64,68,72,76,80,84,88,92,96 = 12 leaps (2000-02-29 is after 2000-01-01, so not counted). 51*365 + 12 = 18615 + 12 = 18627 days. Then 2000-01-01 to 2000-10-01 is 274 days (Jan31+Feb29+Mar31+Apr30+May31+Jun30+Jul31+Aug31+Sep30 = 31+29+31+30+31+30+31+31+30 = 274). So 18627 - 274 = 18353 days from 1949-10-01 to 2000-01-01. 18353 mod 60 = 18353 - 60*305 = 18353 - 18300 = 53. So if 1949-10-01 is index 0 (甲子), then 2000-01-01 is index 53. Index 53: stem = 53 mod 10 = 3 (丁), branch = 53 mod 12 = 5 (巳). So 丁巳日. Hmm, let me check what 2000-01-01 actually is. I believe 2000-01-01 was 戊午日... not sure. Let me use another approach — a known verified anchor. 2024-01-01 was 甲辰日? Hmm. Let me think about a very reliable anchor: JDN-based formula used in Chinese calendar libraries. Common formula (from 《中国天文年历》 style): 干支日 = (儒略日数 + 49) mod 60 ... something. Let me derive from a well-established fact: **1900年1月1日 is 甲戌日**. Hmm, I'm not certain. Another well-known: **1949年10月1日 是 甲子日**. I'm fairly confident about this one — it's widely cited. Actually, let me double-check: many sources state 1949年10月1日 干支为 己丑年 癸酉月 甲子日. Yes, that's commonly cited: 己丑年 癸酉月 甲子日. And 1949年10月1日 was indeed 己丑年 (1949 立春后 = 己丑年 ✓). Let me verify 癸酉月: 1949-10-01. 立秋 was around Aug 8, 白露 around Sep 8, 寒露 around Oct 8. So Oct 1 is in 酉月 (白露 to 寒露). Year stem 己 → 寅月 = 丙寅. 酉 is the 8th month from 寅 (寅=0: 寅卯辰巳午未申酉 = index 7). Stem = 2 + 7 = 9 = 癸. ✓ 癸酉月. So 1949-10-01 = 甲子日 is consistent. Good, I'll use that anchor. Let me define: JDN for 1949-10-01. Julian Day Number (integer, for the day starting at noon). Actually let me use a "day number" convention. I'll compute days since a fixed epoch using a standard algorithm, then anchor. Use the standard: ``` function toJDN(y, m, d) { // Gregorian const a = Math.floor((14 - m) / 12); const y2 = y + 4800 - a; const m2 = m + 12 * a - 3; return d + Math.floor((153 * m2 + 2) / 5) + 365 * y2 + Math.floor(y2 / 4) - Math.floor(y2 / 100) + Math.floor(y2 / 400) - 32045; } ``` This gives JDN at noon for the civil date. 1949-10-01: a = floor((14-10)/12) = 0; y2 = 1949+4800 = 6749; m2 = 10-3 = 7. d=1; floor((153*7+2)/5) = floor(1073/5) = 214; 365*6749 = 2463385; floor(6749/4) = 1687; floor(6749/100)=67; floor(6749/400)=16. JDN = 1 + 214 + 2463385 + 1687 - 67 + 16 - 32045 = 1+214 = 215 215 + 2463385 = 2463600 +1687 = 2465287 -67 = 2465220 +16 = 2465236 -32045 = 2433191. So JDN(1949-10-01) = 2433191, and it's 甲子 (index 0). So dayIndex = (JDN - 2433191) mod 60. But careful: the day pillar changes at 子時 (23:00). The traditional rule: 子時 spans 23:00–01:00, and the day changes at 23:00 (早子時/晚子時 debate). Common practice: 晚子時 (23:00-24:00) belongs to the next day's 子時 in some schools, or same day in others. I'll implement the common convention: the day pillar advances at 23:00 (i.e., 23:00–23:59 counts as the next day's 子時). Actually there are two schools. Let me offer an option, default to "23:00 changes day" (晚子時算次日). Hmm, many popular software defaults to day changing at midnight for the day pillar and 子時 hour stem... Actually the most common modern convention in software (like 万年历) is that the day changes at 23:00. I'll add a toggle. Default: day changes at 23:00. **Hour Pillar**: - 子時 23:00-00:59, 丑時 01:00-02:59, 寅時 03:00-04:59, ... - Hour branch index = floor((hour + 1) / 2) mod 12. - hour 23: (23+1)/2 = 12 mod 12 = 0 = 子 ✓ - hour 0: (0+1)/2 = 0.5 floor = 0 = 子 ✓ - hour 1: 1 → 丑 ✓ - hour 2: 1.5 floor 1 = 丑 ✓ - hour 3: 2 = 寅 ✓ Good. - Hour stem: 五鼠遁 based on day stem: - 甲/己 day → 子時 = 甲子 - 乙/庚 day → 丙子 - 丙/辛 day → 戊子 - 丁/壬 day → 庚子 - 戊/癸 day → 壬子 hourStem = ((dayStem % 5) * 2 + hourBranch) mod 10 Check: 甲(0) → 0+0 = 甲 ✓; 乙(1) → 2 = 丙 ✓; 丙(2) → 4 = 戊 ✓; 丁(3) → 6 = 庚 ✓; 戊(4) → 8 = 壬 ✓; 己(5) → 0 = 甲 ✓. **Decennial Cycles (大運)**: - Direction: 陽年男 / 陰年女 → forward (順行); 陰年男 / 陽年女 → backward (逆行). Based on year stem yin/yang. - Starting age: count days from birth to the next 節 (if forward) or previous 節 (if backward). 3 days = 1 year, 1 day = 4 months, 1 hour = 5 days (approximately). Standard: 3天折合1年, 1天折合4個月, 1時辰(2小時)折合10天... Actually: 3 days = 1 year → 1 day = 4 months → 1 hour = 5 days (since 1 day=12 時辰=4 months=120 days, so 1 時辰 = 10 days, 1 hour = 5 days). Yes. So startAge (in years, decimal) = daysDiff / 3. - The 大運 pillars start from the month pillar, and step forward/backward in the 60-cycle. - Typically 8-10 pillars displayed, each 10 years. 起運年齡 = daysDiff/3 years. The first 大運 starts at that age. Let me now write the code. **Structure**: Single HTML file with embedded CSS and JS. Chinese UI. Features: - Input: 公曆 date, time, gender, timezone (default +8 for China). - Output: Four Pillars (年月日時) with 天干地支, plus 十神? The user said "only displays the Four Pillars and Decennial Cycles". So keep it minimal: just the four pillars and 大運 list. Maybe show 藏干? No — keep it to the four pillars and 大運. I'll display: - 四柱: 年柱, 月柱, 日柱, 時柱 (each 干支) - 大運: list of pillars with starting age and year ranges. Maybe minimal extras like 節氣 info for 起運. I'll keep it clean. Let me write the solar term calculation. ```js const RAD = Math.PI / 180; // VSOP87 truncated series for Earth const L0 = [[175347046,0,0],[3341656,4.6692568,6283.07585],...]; ``` I'll write a function `sunApparentLongitude(jd)`. ```js function earthHelio(jd) { const T = (jd - 2451545.0) / 36525; // L let L = 0; for (let i = 5; i >= 0; i--) { let s = 0; for (const [A,B,C] of Lseries[i]) s += A * Math.cos(B + C*T); L = L * T + s; // hmm, careful } ... } ``` The standard evaluation: L = (L0 + L1*T + L2*T² + L3*T³ + L4*T⁴ + L5*T⁵) / 1e8. Let me just do it directly: ```js function sumSeries(series, T) { let total = 0; for (let i = 0; i < series.length; i++) { let s = 0; for (const [A,B,C] of series[i]) s += A * Math.cos(B + C*T); total += s * Math.pow(T, i); } return total / 1e8; } ``` This is the standard formulation. Then: ```js function sunLongitude(jd) { const T = (jd - 2451545.0) / 36525; const L = sumSeries(L_TERMS, T) * 180/Math.PI; // radians → degrees const B = sumSeries(B_TERMS, T) * 180/Math.PI; const R = sumSeries(R_TERMS, T); // Geocentric let theta = L + 180; // degrees let beta = -B; // FK5 correction const T2 = T*T; const lp = theta - 1.397*T - 0.00031*T2; theta += -0.09033/3600; beta += 0.03916/3600 * (Math.cos(lp*RAD) - Math.sin(lp*RAD)); // Nutation in longitude const dpsi = nutationLongitude(T); // arcsec // Aberration const aberr = -20.4898 / R / 3600; // degrees let lambda = theta + dpsi/3600 + aberr; return ((lambda % 360) + 360) % 360; } ``` Wait, B series for Earth: I didn't include it. The B (latitude) of Earth is very small (max ~1.2"), and its effect on the sun's apparent longitude is negligible for our purposes (it's a second-order effect via the FK5 correction). Actually the sun's geocentric latitude β = -B, and B is at most ~1.2 arcsec. So negligible. I'll skip B and just use B=0. Actually I should include it for correctness of β but it doesn't affect λ much. Skip it. Actually wait — for the sun's apparent longitude we only need L and R. Good. Nutation in longitude (Meeus, simplified, accuracy ~0.5"): ``` Ω = 125.04452 - 1934.136261*T L = 280.4665 + 36000.7698*T (mean longitude of sun) L' = 218.3165 + 481267.8813*T (mean longitude of moon) Δψ = -17.20"sin Ω - 1.32" sin 2L - 0.23" sin 2L' + 0.21" sin 2Ω ``` That's accurate to ~0.5", fine. Now solar term finding. Given a year, find the JD when solar longitude = target degrees. Approach: start with an approximate date. For 立春 (315°) around Feb 4. Use Newton's method: ```js function findSolarTerm(year, targetDeg, approxMonth) { // initial guess let jd = jdFromDate(year, approxMonth, 4); // rough for (let i = 0; i < 20; i++) { const lon = sunLongitude(jd); let diff = targetDeg - lon; // normalize to [-180, 180] while (diff > 180) diff -= 360; while (diff < -180) diff += 360; jd += diff * 365.2422 / 360; if (Math.abs(diff) < 1e-8) break; } return jd; } ``` This should converge quickly. Better: use a lookup of approximate day-of-year for each term, then Newton. Approximate dates for the 12 節 in the Northern Hemisphere: - 小寒 285° ~ Jan 6 - 立春 315° ~ Feb 4 - 驚蟄 345° ~ Mar 6 - 清明 15° ~ Apr 5 - 立夏 45° ~ May 6 - 芒種 75° ~ Jun 6 - 小暑 105° ~ Jul 7 - 立秋 135° ~ Aug 8 - 白露 165° ~ Sep 8 - 寒露 195° ~ Oct 8 - 立冬 225° ~ Nov 7 - 大雪 255° ~ Dec 7 Now, JD to calendar conversion. I need to handle timezone. Let me work in local time (UTC+8 default) and convert. Actually simpler: work with a JS Date (which is UTC internally) and convert. Let me define: - `dateToJD(date)` = date.getTime()/86400000 + 2440587.5 - `jdToDate(jd)` = new Date((jd - 2440587.5) * 86400000) For the user input, I'll take year/month/day/hour/minute in a specified timezone offset, and compute the UTC time. ```js const tzOffset = 8; // hours const utcMs = Date.UTC(y, m-1, d, hh, mm) - tzOffset*3600000; ``` Then jd = utcMs/86400000 + 2440587.5. For solar terms, the found JD is in TT (dynamical time) roughly, but ΔT is ~70s, negligible-ish. Actually the VSOP87 gives TT-based positions. We should subtract ΔT to get UT. ΔT is about 70 seconds for modern era — that's 0.0008 days, ~1 minute. For solar term boundary determination, could matter at the minute level but generally fine. I'll ignore it or apply a rough ΔT. Let me apply a rough ΔT correction for accuracy. Actually, for simplicity and since it's ~1 minute, I'll apply an approximation: ```js function deltaT(year) { // approximate, seconds if (year >= 2005 && year <= 2050) { const t = year - 2000; return 62.92 + 0.32217*t + 0.005589*t*t; } ... } ``` Meh. Let me include a simple polynomial for 1900-2150 and just use it. Actually, I'll skip ΔT — the error is ~1 minute which only matters if birth is within 1 minute of a solar term boundary. But let's include it anyway for professionalism... Hmm, it adds complexity. Let me include a simple version. Actually, I'll include it. Simple piecewise: ```js function deltaTsec(y) { // Meeus/NASA approximations if (y < -500) {...} ... } ``` Too much. Let me use the NASA polynomial for 2005-2050 and a simpler one outside. Or just use: ΔT ≈ 69 seconds constant for 2020s. Actually let me use a decent approximation: ```js function deltaT(year, month) { const y = year + (month - 0.5) / 12; let t; if (y >= 2005 && y < 2050) { t = y - 2000; return 62.92 + 0.32217*t + 0.005589*t*t; } if (y >= 1986 && y < 2005) { t = y - 2000; return 63.86 + 0.3345*t - 0.060374*t*t + 0.0017275*t**3 + 0.000651814*t**4 + 0.00002373599*t**5; } if (y >= 1961 && y < 1986) { t = y - 1975; return 45.45 + 1.067*t - t*t/260 - t**3/718; } if (y >= 1941 && y < 1961) { t = y - 1950; return 29.07 + 0.407*t - t*t/233 + t**3/2547; } if (y >= 1920 && y < 1941) { t = y - 1920; return 21.20 + 0.84493*t - 0.076100*t*t + 0.0020936*t**3; } if (y >= 1900 && y < 1920) { t = y - 1900; return -2.79 + 1.494119*t - 0.0598939*t*t + 0.0061966*t**3 - 0.000197*t**4; } if (y >= 2050 && y < 2150) { const u = (y-1820)/100; return -20 + 32*u*u - 0.5628*(2150-y); } // fallback const u = (y - 1820) / 100; return -20 + 32*u*u; } ``` Good enough. So: to find solar term time in UT: find jd_tt where longitude = target, then jd_ut = jd_tt - ΔT/86400. Hmm, but actually the sun's position computed from jd_tt. And when we search, we're finding the TT instant. Then convert to UT. OK let me be careful: `sunLongitude(jd)` where jd is TT. So the result of the search is jd_tt. Then jd_ut = jd_tt - deltaT/86400. Fine. Now the algorithm for the chart: 1. Compute jd_ut from birth local time. 2. jd_tt = jd_ut + ΔT/86400. 3. Find the year's 立春: need to determine which 立春 the birth is after. - Compute 立春 for year Y-1, Y, Y+1 (Gregorian year of birth). - BaZi year = the largest Y' such that 立春(Y') <= birth. 4. Month: find the 節 boundaries. Compute all 12 節 for the relevant range and find which interval contains birth. Simpler: compute solar longitude at birth, then determine month index from the longitude. - 315° ≤ λ < 345° → 寅月 (index 0) - 345° ≤ λ < 15° → 卯月 (1) - 15° ≤ λ < 45° → 辰月 (2) - 45° ≤ λ < 75° → 巳月 (3) - 75° ≤ λ < 105° → 午月 (4) - 105° ≤ λ < 135° → 未月 (5) - 135° ≤ λ < 165° → 申月 (6) - 165° ≤ λ < 195° → 酉月 (7) - 195° ≤ λ < 225° → 戌月 (8) - 225° ≤ λ < 255° → 亥月 (9) - 255° ≤ λ < 285° → 子月 (10) - 285° ≤ λ < 315° → 丑月 (11) So monthIndex = floor(((λ - 315 + 360) % 360) / 30). Check: λ=315 → 0 ✓; λ=345 → 30/30=1 ✓; λ=0 → (0-315+360)=45 → 45/30=1.5 floor 1 = 卯 ✓; λ=15 → (15-315+360)=60 → 2 = 辰 ✓. But careful: using longitude directly vs. term times — equivalent since the terms are defined by longitude. Good, simpler and more accurate. However, for the year boundary, I need 立春 specifically. Using longitude: if λ is in [315, 360) or [0, 315)... Hmm. Actually, the "BaZi year" changes at 立春. So: - If the birth is after 立春 of Gregorian year Y (i.e., in terms of time), BaZi year = Y. Using the term times is cleaner. Let me compute 立春 times. Actually, there's a subtlety: near the poles of the year, e.g., birth on Jan 15 — λ is around 295° (丑月), and the BaZi year is the previous year. So I need 立春 times. Let me just compute: lichunThisYear = findTerm(Y, 315). If birth >= lichunThisYear, baziYear = Y, else baziYear = Y-1. But this uses the Gregorian year Y from the birth date. Since 立春 is around Feb 4, and the birth is either before or after it within year Y — that works. 5. Year stem/branch: - 1984 = 甲子. yearIndex = ((baziYear - 1984) % 60 + 60) % 60. - stem = yearIndex % 10, branch = yearIndex % 12. 6. Month stem/branch as described. 7. Day pillar from JDN. 8. Hour pillar. 9. 大運: - yangYear = (yearStem % 2 === 0) // 甲丙戊庚壬 = 陽 - forward = (yangYear && male) || (!yangYear && !male) - Find the next 節 after birth (if forward) or previous 節 before birth (if backward). - "節" here means the 12 major terms (節氣中的"節"). But actually, for 大運起運, the standard is to count to the next/previous 節 (月令節氣), which are exactly the 12 terms we listed. - daysDiff = |termJD - birthJD| (in days) - startAgeYears = daysDiff / 3 - The 大運 sequence: start from month pillar, step ±1 in the 60-cycle. - Each 大運 lasts 10 years. - Display: 起運 age (years, months, days), then each pillar with start age and the Gregorian year range. Let me compute startAge as years + months + days: - totalDays = daysDiff - 3 days = 1 year → years = floor(totalDays / 3) - remainder days: rem = totalDays - years*3; 1 day = 4 months → months = floor(rem * 4); rem2 = rem*4 - months; 1 month = 30 days → days = round(rem2 * 30) Hmm, actually the traditional conversion: 3天=1年, 1天=4個月, 1時辰=10天 (2 hours = 10 days). So 1 hour = 5 days. Equivalent to: 1 day = 4 months = 120 days. So rem (in days) → rem*4 months → fractional months * 30 days. Let me do: totalDays → years = floor(totalDays/3), remDays = totalDays - 3*years (0..3). months = floor(remDays * 4), remMonths = remDays*4 - months (0..1). days = round(remMonths * 30). OK. Now, the 大運 start date: birth date + startAge. And each subsequent pillar every 10 years. Let me now also handle: should I display 8 or 10 pillars? Traditional: 8 大運 (80 years) or 10. I'll show 10. Now let's write the code. Also need the 天干 and 地支 names, plus 五行 maybe. Keep it minimal but nice. Let me write out the arrays: ```js const GAN = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']; const ZHI = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']; ``` Now let's write the whole thing. ```html 八字排盘 — 四柱与大运 ... ``` Let me write the JS carefully. ```js /* ============ 基础常量 ============ */ const GAN = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']; const ZHI = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']; const RAD = Math.PI / 180; /* ============ VSOP87 截断系数(地球) ============ */ const EARTH_L = [ // L0 [[175347046,0,0],[3341656,4.6692568,6283.07585],[34894,4.6261,12566.1517], [3497,2.7441,5753.3849],[3418,2.8289,3.5231],[3136,3.6277,77713.7715], [2676,4.4181,7860.4194],[2343,6.1352,3930.2097],[1324,0.7425,11506.7698], [1273,2.0371,529.691],[1199,1.1096,1577.3435],[990,5.233,5884.927], [902,2.045,26.298],[857,3.508,398.149],[780,1.179,5223.694], [753,2.533,5507.553],[505,4.583,18849.228],[492,4.205,775.523], [357,2.92,0.067],[317,5.849,11790.629],[284,1.899,796.298], [271,0.315,10977.079],[243,0.345,5486.778],[206,4.806,2544.314], [205,1.869,5573.143],[202,2.458,6069.777],[156,0.833,213.299], [132,3.411,2942.463],[126,1.083,20.775],[115,0.645,0.98], [103,0.636,4694.003],[102,0.976,15720.839],[102,4.267,7.114], [99,6.21,2146.17],[98,0.68,155.42],[86,5.98,161000.69], [85,1.3,6275.96],[85,3.67,71430.7],[80,1.81,17260.15], [79,3.04,12036.46],[75,1.76,5088.63],[74,3.5,3154.69], [74,4.68,801.82],[70,0.83,9437.76],[62,3.98,8827.39], [61,1.82,7084.9],[57,2.78,6286.6],[56,4.39,14143.5], [56,3.47,6279.55],[52,0.19,12139.55],[52,1.33,1748.02], [51,0.28,5856.48],[49,0.49,1194.45],[41,5.37,8429.24], [41,2.4,19651.05],[39,6.17,10447.39],[37,6.04,10213.29], [37,2.57,1059.38],[36,1.71,2352.87],[36,1.78,6812.77], [33,0.59,17789.85],[30,0.44,83996.85],[30,2.74,1349.87], [25,3.16,4690.48]], // L1 [[628331966747,0,0],[206059,2.678235,6283.07585],[4303,2.6351,12566.1517], [425,1.59,3.523],[119,5.796,26.298],[109,2.966,1577.344], [93,2.59,18849.23],[72,1.14,529.69],[68,1.87,398.15], [67,4.41,5507.55],[59,2.89,5223.69],[56,2.17,155.42], [45,0.4,796.3],[36,0.47,775.52],[29,2.65,7.11], [21,5.34,0.98],[19,1.85,5486.78],[19,4.97,213.3], [17,2.99,6275.96],[16,0.03,2544.31],[16,1.43,2146.17], [15,1.21,10977.08],[12,2.83,1748.02],[12,3.26,5088.63], [12,5.27,1194.45],[12,2.08,4694],[11,0.77,553.57], [10,1.3,6286.6],[10,4.24,1349.87],[9,2.7,242.73], [9,5.64,951.72],[8,5.3,2352.87],[6,2.65,9437.76], [6,4.67,4690.48]], // L2 [[52919,0,0],[8720,1.0721,6283.0758],[309,0.867,12566.152], [27,0.05,3.52],[16,5.19,26.3],[16,3.68,155.42], [10,0.76,18849.23],[9,2.06,77713.77],[7,0.83,775.52], [5,4.66,1577.34],[4,1.03,7.11],[4,3.44,5573.14], [3,5.14,796.3],[3,6.05,5507.55],[3,1.19,242.73], [3,6.12,529.69],[3,0.31,398.15],[3,2.28,553.57], [2,4.38,5223.69],[2,3.75,0.98]], // L3 [[289,5.844,6283.076],[35,0,0],[17,5.49,12566.15], [3,5.2,155.42],[1,4.72,3.52],[1,5.3,18849.23],[1,5.97,242.73]], // L4 [[114,3.142,0],[8,4.13,6283.08],[1,3.84,12566.15]], // L5 [[1,3.14,0]] ]; const EARTH_R = [ [[100013989,0,0],[1670700,3.0984635,6283.07585],[13956,3.05525,12566.1517], [3084,5.1985,77713.7715],[1628,1.1739,5753.3849],[1576,2.8469,7860.4194], [925,5.453,11506.77],[542,4.564,3930.21],[472,3.661,5884.927], [346,0.964,5507.553],[329,5.9,5223.694],[307,0.299,5573.143], [243,4.273,11790.629],[212,5.847,1577.344],[186,5.022,10977.079], [175,3.012,18849.228],[110,5.055,5486.778],[98,0.89,6069.78], [86,5.69,15720.84],[86,1.27,161000.69],[65,0.27,17260.15], [63,0.92,529.69],[57,2.01,83996.85],[56,5.24,71430.7], [49,3.25,2544.31],[47,2.58,775.52],[45,5.54,9437.76], [43,6.01,6275.96],[39,5.36,4694],[38,2.39,8827.39], [37,0.83,19651.05],[37,4.9,12139.55],[36,1.67,12036.46], [35,1.84,2942.46],[33,0.24,7084.9],[32,0.18,5088.63], [32,1.78,398.15],[28,1.21,6286.6],[28,1.9,6279.55], [26,4.59,10447.39]], [[103019,1.10749,6283.07585],[1721,1.0644,12566.1517],[702,3.142,0], [32,1.02,18849.23],[31,2.84,5507.55],[25,1.32,5223.69], [18,1.42,1577.34],[10,5.91,10977.08],[9,1.42,6275.96],[9,0.27,5486.78]], [[4359,5.7846,6283.0758],[124,5.579,12566.152],[12,3.14,0], [9,3.63,77713.77],[6,1.87,5573.14],[3,5.47,18849.23]], [[145,4.273,6283.076],[7,3.92,12566.15]], [[4,2.56,6283.08]] ]; ``` Now functions: ```js function seriesSum(terms, T) { let total = 0; for (let i = 0; i < terms.length; i++) { let s = 0; const arr = terms[i]; for (let j = 0; j < arr.length; j++) s += arr[j][0] * Math.cos(arr[j][1] + arr[j][2] * T); total += s * Math.pow(T, i); } return total / 1e8; } ``` ```js function nutationLongitude(T) { const om = (125.04452 - 1934.136261 * T) * RAD; const L = (280.4665 + 36000.7698 * T) * RAD; const Lp = (218.3165 + 481267.8813 * T) * RAD; return -17.20*Math.sin(om) - 1.32*Math.sin(2*L) - 0.23*Math.sin(2*Lp) + 0.21*Math.sin(2*om); } ``` ```js function sunApparentLongitude(jdTT) { const T = (jdTT - 2451545.0) / 36525; const L = seriesSum(EARTH_L, T) / RAD; // radians → degrees: divide by RAD (since RAD = pi/180) const R = seriesSum(EARTH_R, T); let theta = L + 180; // FK5 const lp = theta - 1.397 * T - 0.00031 * T * T; theta -= 0.09033 / 3600; // (latitude correction omitted — negligible for longitude) const dpsi = nutationLongitude(T); // arcsec const aberr = -20.4898 / R; // arcsec let lambda = theta + (dpsi + aberr) / 3600; return ((lambda % 360) + 360) % 360; } ``` Wait: `seriesSum(EARTH_L, T)` returns the value in radians (since the VSOP coefficients are in radians). So converting to degrees: `* 180/Math.PI` = `/= RAD`. Yes. Now ΔT: ```js function deltaTSeconds(y, m) { const Y = y + (m - 0.5) / 12; let t; if (Y >= 2005 && Y < 2050) { t = Y - 2000; return 62.92 + 0.32217*t + 0.005589*t*t; } if (Y >= 1986 && Y < 2005) { t = Y - 2000; return 63.86 + 0.3345*t - 0.060374*t*t + 0.0017275*t**3 + 0.000651814*t**4 + 0.00002373599*t**5; } if (Y >= 1961 && Y < 1986) { t = Y - 1975; return 45.45 + 1.067*t - t*t/260 - t**3/718; } if (Y >= 1941 && Y < 1961) { t = Y - 1950; return 29.07 + 0.407*t - t*t/233 + t**3/2547; } if (Y >= 1920 && Y < 1941) { t = Y - 1920; return 21.20 + 0.84493*t - 0.076100*t*t + 0.0020936*t**3; } if (Y >= 1900 && Y < 1920) { t = Y - 1900; return -2.79 + 1.494119*t - 0.0598939*t*t + 0.0061966*t**3 - 0.000197*t**4; } if (Y >= 2050 && Y < 2150) { const u = (Y - 1820)/100; return -20 + 32*u*u - 0.5628*(2150 - Y); } const u = (Y - 1820) / 100; return -20 + 32*u*u; } ``` Now solar term search: ```js function solarTermJD(year, targetDeg) { // 初值:按平均日期估算 const approxDays = [6,4,6,5,6,6,7,8,8,8,7,7]; // 小寒..大雪 rough day // targetDeg: 285(小寒),315(立春),345(惊蛰),15,45,75,105,135,165,195,225,255 ... } ``` Hmm, let me just do a generic solver: given a target longitude and an approximate JD, Newton iterate. ```js function solveLongitude(targetDeg, jdGuess) { let jd = jdGuess; for (let i = 0; i < 30; i++) { const lon = sunApparentLongitude(jd); let diff = targetDeg - lon; while (diff > 180) diff -= 360; while (diff < -180) diff += 360; jd += diff * 365.2422 / 360; if (Math.abs(diff) < 1e-9) break; } return jd; } ``` For the guess: For term with longitude λ in Gregorian year Y, approximate date. Let's define: The 節 with longitude 285 (小寒) occurs ~Jan 6 of year Y. 315 (立春) ~ Feb 4 345 (驚蟄) ~ Mar 6 15 (清明) ~ Apr 5 45 (立夏) ~ May 6 75 (芒種) ~ Jun 6 105 (小暑) ~ Jul 7 135 (立秋) ~ Aug 8 165 (白露) ~ Sep 8 195 (寒露) ~ Oct 8 225 (立冬) ~ Nov 7 255 (大雪) ~ Dec 7 So I can map targetDeg → (month, day) guess. ```js const TERM_INFO = { 285: [1, 6], 315: [2, 4], 345: [3, 6], 15: [4, 5], 45: [5, 6], 75: [6, 6], 105: [7, 7], 135: [8, 8], 165: [9, 8], 195: [10, 8], 225: [11, 7], 255: [12, 7] }; ``` Then: ```js function termJD(year, deg) { const [mo, dy] = TERM_INFO[deg]; let guess = jdFromUTCDate(year, mo, dy, 0, 0); return solveLongitude(deg, guess); // returns TT jd } ``` Then convert to UT: `jdUT = jdTT - deltaT/86400`. Hmm, but the guess should be in TT too. It's fine, close enough. `jdFromUTCDate`: ```js function dateToJD(date) { return date.getTime() / 86400000 + 2440587.5; } function jdToDate(jd) { return new Date((jd - 2440587.5) * 86400000); } ``` Now the main chart function. ```js function buildChart(opts) { const { year, month, day, hour, minute, gender, tz } = opts; // 出生时刻的 UTC 毫秒 const birthMs = Date.UTC(year, month-1, day, hour, minute, 0) - tz*3600000; const jdUT = birthMs / 86400000 + 2440587.5; const jdTT = jdUT + deltaTSeconds(year, month) / 86400; const sunLon = sunApparentLongitude(jdTT); // 年柱 const lichunJD = termJD_UT(year, 315); const baziYear = (jdUT >= lichunJD) ? year : year - 1; const yIdx = ((baziYear - 1984) % 60 + 60) % 60; const yearGan = yIdx % 10, yearZhi = yIdx % 12; // 月柱 const monthOffset = Math.floor((((sunLon - 315) % 360) + 360) % 360 / 30); const monthZhi = (2 + monthOffset) % 12; // 寅=2 const monthGan = ((yearGan % 5) * 2 + 2 + monthOffset) % 10; // 日柱 // 以 1949-10-01 为甲子日 ... } ``` For the day pillar: I need the local date at the birth place, adjusted for the 23:00 rule. ```js // 本地时间 const localMs = birthMs + tz*3600000; // = Date.UTC(...) // 直接构造本地日期 ``` Actually, let me handle this: compute the "day number" using the local calendar date and local hour. ```js let d = day, mo = month, y = year; if (hour >= 23) { /* 日柱进一位(晚子时算次日) */ advance one day } ``` Hmm, but if I advance the date, the day pillar is computed for the next calendar day. Let me just compute the JDN of the local date, then add 1 if hour >= 23 (and the convention is enabled). ```js let jdn = toJDN(year, month, day); if (hour >= 23 && dayChangesAt23) jdn += 1; const dayIdx = ((jdn - 2433191) % 60 + 60) % 60; const dayGan = dayIdx % 10, dayZhi = dayIdx % 12; ``` Wait, careful — for the hour pillar's 五鼠遁, we use the day stem. With 晚子時, the day stem used for the hour pillar should be the new day's stem (that's the standard for 晚子時算次日). Yes, consistent. Hmm, but there's a subtlety: some schools say 23:00-24:00 uses the current day's stem for the hour pillar but the next day's stem for the day pillar. I'll use the consistent approach: 日柱 and 時柱 both use the advanced day. Let me make the toggle: "子時換日" (23:00 換日) vs "00:00 換日". - If 23:00 換日: hour >= 23 → next day for both day and hour pillars. - If 00:00 換日: day changes at midnight; 23:00-23:59 is 子時 of the current day (晚子時), and the hour stem uses the current day's stem. I'll implement both. ```js // 時柱 const hourZhi = Math.floor(((hour + 1) % 24) / 2); // 23→0, 0→0, 1→1, ... ``` Wait: hour=23 → (24)%24 = 0 → 0/2 = 0 → 子 ✓ hour=0 → 1%24=1 → 0.5 → floor 0 → 子 ✓ hour=1 → 2 → 1 → 丑 ✓ hour=2 → 3 → 1.5 → 1 → 丑 ✓ hour=3 → 4 → 2 → 寅 ✓ Good. ```js const hourGan = ((dayGan % 5) * 2 + hourZhi) % 10; ``` Now 大運: ```js const yangYear = (yearGan % 2 === 0); const isMale = gender === 'M'; const forward = (yangYear === isMale); // 找到 birth 之后/之前的 "節" // 12 个节的经度 const JIE = [315, 345, 15, 45, 75, 105, 135, 165, 195, 225, 255, 285]; // 寅..丑 let targetJD; if (forward) { // 下一个节 targetJD = null; for (const deg of ...) find the smallest termJD_UT > birthJD } else { // 上一个节 } ``` Simplest: compute all 節 times within a window (e.g., from year-2 to year+2), collect them, sort, then find the nearest after/before. ```js const terms = []; for (let y = year - 1; y <= year + 1; y++) { for (const deg of JIE) { terms.push({ deg, jd: termJD_UT(y, deg) }); } } terms.sort((a,b) => a.jd - b.jd); let nextTerm = null, prevTerm = null; for (const t of terms) { if (t.jd > jdUT) { nextTerm = t; break; } } for (let i = terms.length-1; i >= 0; i--) { if (terms[i].jd <= jdUT) { prevTerm = terms[i]; break; } } const diffDays = forward ? (nextTerm.jd - jdUT) : (jdUT - prevTerm.jd); const startAge = diffDays / 3; ``` Then the 大運 pillars: ```js const monthIdx = ((monthGan - monthZhi) ... ) ``` Hmm, the month pillar's index in the 60-cycle. Let me compute it properly. The 60-cycle index i where i%10 = gan, i%12 = zhi. Given gan g and zhi z, find i in [0,59]. Solution: i ≡ g (mod 10), i ≡ z (mod 12). Since gcd(10,12)=2, a solution exists only if g ≡ z (mod 2). For valid 干支 it always holds. ```js function ganzhiIndex(g, z) { for (let i = 0; i < 60; i++) if (i % 10 === g && i % 12 === z) return i; return -1; } ``` Simple loop, fine. Then 大運 pillars: `(monthIdx + k*dir) mod 60` for k = 1..10. Display: 起運 at age startAge, first 大運 pillar runs from startAge to startAge+10, etc. Let me also compute the actual calendar year when each 大運 starts: birthYear + floor(startAge + 10*k). Actually more precisely: the start date = birth date + startAge years. Let me compute approximate Gregorian years. Let me now design the output. **Layout:** ``` [输入区] 公历日期时间, 性别, 时区, 子时换日选项 [排盘按钮] [结果区] 四柱: 年柱 月柱 日柱 时柱 甲 乙 丙 丁 (天干) 子 丑 寅 卯 (地支) 大运: 起运: X岁Y个月Z天 顺行/逆行 表格: 序号 | 干支 | 起始年龄 | 起始年份 ``` Let me make it look nice with CSS. Also add 五行 colors for the 天干地支? Might be nice but "only displays the Four Pillars and Decennial Cycles" — I'll keep it minimal but with color coding for readability. Actually, adding 五行 colors is a display nicety, not extra info. Let me add subtle colors. Hmm, the user said "only displays the Four Pillars and Decennial Cycles" — meaning don't add 十神, 藏干, 纳音, 神煞, 流年, etc. I'll stick to that. Let me write the final code. One more consideration: the default date should be something sensible, e.g., current date/time. Let me also double check the 立春 year calculation. If birth is Jan 15, 2000: jdUT < lichun(2000) which is ~Feb 4, 2000. So baziYear = 1999. yIdx = (1999-1984) = 15. 15 % 10 = 5 = 己, 15 % 12 = 3 = 卯. So 己卯年. 1999 is 己卯 ✓. For 2024: (2024-1984) = 40. 40%10 = 0 = 甲, 40%12 = 4 = 辰. 甲辰 ✓ (2024 is 甲辰年). Good. Now the month: for 2024-02-10 (after 立春 Feb 4), sunLon ≈ 320.9°. monthOffset = floor((320.9-315)/30) = floor(0.197) = 0 → 寅月. monthZhi = 2 = 寅 ✓. monthGan = ((0%5)*2 + 2 + 0) % 10 = 2 = 丙. So 丙寅月 ✓ (2024 甲辰年 正月 = 丙寅月). Now let me verify the day pillar with a known date. 2024-01-01: JDN? a = floor((14-1)/12) = 1; y2 = 2024+4800-1 = 6823; m2 = 1+12-3 = 10. JDN = 1 + floor((153*10+2)/5) + 365*6823 + floor(6823/4) - floor(6823/100) + floor(6823/400) - 32045 = 1 + floor(1532/5) + 2490395 + 1705 - 68 + 17 - 32045 = 1 + 306 + 2490395 + 1705 - 68 + 17 - 32045 = 1+306 = 307 307 + 2490395 = 2490702 +1705 = 2492407 -68 = 2492339 +17 = 2492356 -32045 = 2460311. dayIdx = (2460311 - 2433191) % 60 = 27120 % 60 = 27120 / 60 = 452 exactly → 0. So 2024-01-01 = 甲子日! Hmm, is that right? Let me check: I recall 2024-01-01 was 甲子日... Actually let me verify differently. Known: 2024-02-10 (春节) was 甲辰年 丙寅月 甲辰日? Hmm. Let me compute: from 2024-01-01 (甲子, idx 0) to 2024-02-10 = 40 days. idx = 40. 40 % 10 = 0 = 甲, 40 % 12 = 4 = 辰. So 甲辰日. I do recall 2024年2月10日 (春节) was 甲辰日. Yes! I'm fairly confident. Good, the anchor works. Let me double-check the 1949-10-01 anchor once more with another known date. 2000-01-01: JDN? a = floor((14-1)/12) = 1; y2 = 2000+4800-1 = 6799; m2 = 1+12-3 = 10. JDN = 1 + floor((153*10+2)/5) + 365*6799 + floor(6799/4) - floor(6799/100) + floor(6799/400) - 32045 = 1 + 306 + 2481635 + 1699 - 67 + 16 - 32045 = 307 + 2481635 = 2481942 +1699 = 2483641 -67 = 2483574 +16 = 2483590 -32045 = 2451545. That's the J2000.0 epoch JDN ✓ (2000-01-01 12:00 TT = JD 2451545.0). dayIdx = (2451545 - 2433191) % 60 = 18354 % 60 = 18354 - 18300 = 54. 54 % 10 = 4 = 戊, 54 % 12 = 6 = 午. So 2000-01-01 = 戊午日. Hmm, earlier I computed 53 using a different day count. Let me recheck: I said 18353 days between 1949-10-01 and 2000-01-01, but the JDN difference is 18354. Let me recount. From 1949-10-01 to 2000-01-01: Years: 1949-10-01 to 1999-10-01 = 50 years. Leap days in that span: 1952, 1956, ..., 1996 = 12. Wait, 1950-10-01 to 1999-10-01 spans... Let me just trust the JDN. JDN difference = 2451545 - 2433191 = 18354. And 1949-10-01 → 2000-01-01. Let me verify: 1949-10-01 to 1999-10-01 = 50 years with leap days 1952,56,60,64,68,72,76,80,84,88,92,96 = 12 → 50*365+12 = 18262. Then 1999-10-01 to 2000-01-01 = 31(Oct) + 30(Nov) + 31(Dec) + 1 = 93. 18262 + 93 = 18355. Hmm, off by one. Hmm. Let me redo. From 1999-10-01 to 2000-01-01: Oct 1 → Nov 1: 31 days Nov 1 → Dec 1: 30 days Dec 1 → Jan 1: 31 days Total = 92 days. 18262 + 92 = 18354. ✓ So 2000-01-01 is 戊午日 (idx 54). Let me sanity check with an online-known fact... I believe 2000年1月1日 是 戊午日. Hmm, actually I'm not sure. Let me check another way. Known: 2024-01-01 = 甲子日. Let's verify with an independent memory: 2024年1月1日 农历十一月二十, 干支... I've seen 甲子日 mentioned for 2024-01-01. Hmm. Let me cross-check with 1949-10-01 = 甲子日 claim. From 1949-10-01 to 2024-01-01: JDN diff = 2460311 - 2433191 = 27120. 27120/60 = 452. Exactly divisible ✓. So they're the same 干支. If 1949-10-01 is 甲子, then 2024-01-01 is 甲子. Both are commonly cited. I'm reasonably confident. Actually, let me verify 1949-10-01 = 甲子日 more. Hmm, some sources say 1949年10月1日 干支是 甲子日. Yes, I've seen this in multiple places. OK. Let me also verify with a third: 2023-01-01. JDN diff from 2024-01-01 = 365. 365 % 60 = 5. So 2023-01-01 idx = (0 - 5 + 60) % 60 = 55. 55 % 10 = 5 = 己, 55 % 12 = 7 = 未. 己未日. Hmm, I think 2023年1月1日 was 己未日. Plausible. OK, I'll go with it. Now, let me also think about whether the day pillar should use the local date at the birthplace. Yes — the day pillar is based on the local calendar day. If someone is born in a different timezone, the local date matters. So I'll use the entered local date/time and timezone. Alright, but there's a subtlety with the JDN: the JDN formula uses the proleptic Gregorian calendar. For dates before 1582, it'd be the Julian calendar, but that's an edge case. I'll note it or just use the Gregorian proleptic. Fine. Now let's write the UI. Let me write clean, complete code. ```html 八字排盘 · 四柱与大运 ``` Let me write the whole thing. For the four pillars display, I'll use a 4-column grid: ``` 年柱 月柱 日柱 时柱 天干 甲 丙 甲 甲 地支 辰 寅 辰 子 ``` For 大运, a horizontal row of pillars with ages below. Let me now write it all out. ```js /* ---------- 干支 ---------- */ const GAN = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']; const ZHI = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']; const GAN_WX = ['木','木','火','火','土','土','金','金','水','水']; const ZHI_WX = ['水','土','木','木','土','火','火','土','金','金','土','水']; ``` Colors: 木 green, 火 red, 土 brown/yellow, 金 gold/white, 水 blue. OK, writing it now. One more thing: I should handle the input of time zone. Default +8. Also allow negative. And "真太阳时" correction? That's an extra feature. The user said only Four Pillars and Decennial Cycles. I'll skip 真太阳时 to keep it focused. Hmm, but it might be expected. No, keep it minimal per the request. Let me write out the final code. ```js /* ============ 天文计算 ============ */ const RAD = Math.PI/180; const EARTH_L = [...]; const EARTH_R = [...]; function vsopSum(series, T){ let total = 0; for(let i=0;i 180) diff -= 360; while(diff < -180) diff += 360; if(Math.abs(diff) < 1e-10) break; jd += diff * 365.2422 / 360; } // jd 为 TT,转为 UT return jd - deltaTSeconds(year, g[0]) / 86400; } ``` Good. Now the main function. ```js function calcChart(input){ const {y, mo, d, h, mi, tz, male, lateZi} = input; const birthMs = Date.UTC(y, mo-1, d, h, mi, 0) - tz*3600000; const jdUT = birthMs/86400000 + 2440587.5; const jdTT = jdUT + deltaTSeconds(y, mo)/86400; const sunLon = sunApparentLongitude(jdTT); // ---- 年柱 ---- const lichun = termJD(y, 315); const baziYear = jdUT >= lichun ? y : y-1; const yIdx = ((baziYear - 1984) % 60 + 60) % 60; const yearGan = yIdx % 10, yearZhi = yIdx % 12; // ---- 月柱 ---- const mOff = Math.floor((((sunLon - 315) % 360) + 360) % 360 / 30); const monthZhi = (2 + mOff) % 12; const monthGan = ((yearGan % 5)*2 + 2 + mOff) % 10; // ---- 日柱 ---- let jdn = toJDN(y, mo, d); let dayGan, dayZhi, hourZhi, hourGan; const ziShi = (h >= 23 || h < 1); // 子时 if(lateZi && h >= 23) jdn += 1; const dIdx = ((jdn - 2433191) % 60 + 60) % 60; dayGan = dIdx % 10; dayZhi = dIdx % 12; // ---- 时柱 ---- hourZhi = Math.floor(((h + 1) % 24) / 2); hourGan = ((dayGan % 5)*2 + hourZhi) % 10; ... } ``` Wait — there's a bug: `Math.floor(((h+1)%24)/2)`. For h=23: (24)%24=0 → 0 ✓. For h=0: 1 → 0.5 → 0 ✓. For h=22: 23 → 11.5 → 11 = 亥 ✓. Good. Now the 大運. ```js const JIE = [315, 345, 15, 45, 75, 105, 135, 165, 195, 225, 255, 285]; const terms = []; for(let yy = y-1; yy <= y+1; yy++) for(const deg of JIE) terms.push({deg, jd: termJD(yy, deg)}); terms.sort((a,b)=>a.jd-b.jd); let nextTerm = null, prevTerm = null; for(const t of terms){ if(t.jd > jdUT){ nextTerm = t; break; } } for(let i = terms.length-1; i>=0; i--){ if(terms[i].jd <= jdUT){ prevTerm = terms[i]; break; } } const yangYear = yearGan % 2 === 0; const forward = (yangYear === male); // 阳男阴女顺行 const diffDays = forward ? (nextTerm.jd - jdUT) : (jdUT - prevTerm.jd); const startAgeYears = diffDays / 3; ``` Hmm, careful: if `male` is a boolean and `yangYear` is a boolean, then `forward = (yangYear === male)`. 阳年男 → true === true → true ✓. 阴年男 → false === true → false ✓ (逆行). 阳年女 → true === false → false ✓. 阴年女 → false === false → true ✓. Then: ```js const monthGZ = ganzhiIndex(monthGan, monthZhi); const dir = forward ? 1 : -1; const luck = []; for(let k=1;k<=10;k++){ const idx = ((monthGZ + dir*k) % 60 + 60) % 60; luck.push({gan: idx%10, zhi: idx%12, startAge: startAgeYears + (k-1)*10}); } ``` Then compute the Gregorian year for each: birthYear + startAge + (k-1)*10, roughly. Better: compute the actual date = birth date + startAge years. Let me just add fractional years to the birth date. ```js const birthDate = new Date(Date.UTC(y, mo-1, d, h, mi)); // 大运起始时间(近似) function addYears(date, yrs){ const ms = date.getTime() + yrs * 365.2425 * 86400000; return new Date(ms); } ``` And display the year. Now the age formatting: ```js function formatAge(totalYears){ const years = Math.floor(totalYears); const remDays = (totalYears - years) * 3; // 剩余天数(0~3) const months = Math.floor(remDays * 4); const remM = remDays*4 - months; const days = Math.round(remM * 30); return {years, months, days}; } ``` Hmm, careful: totalYears = diffDays/3. years = floor(totalYears). remDays = (totalYears - years)*3 = the leftover days. Then months = floor(remDays*4), days = round((remDays*4 - months)*30). Yes. Now let me put together the display. Let me write the HTML structure: ```html

八 字 排 盘

四柱 · 大运
``` Let me write the render function. ```js function render(res){ const {pillars, luck, meta} = res; ... } ``` Alright, let me write everything out now in final form. I'll also add a small "节气/起运" info line. Actually, let me keep it minimal but include the 起运 info since it's part of 大运. Let me write the final code. For the four pillars table, I'll do: ```html
年柱
...
``` Actually a grid layout: ```html
年柱
月柱
日柱
时柱
天干
甲
...
地支
辰
...
``` Using CSS grid: `grid-template-columns: 60px repeat(4, 1fr)`. Hmm, but I'd rather display 天干 above 地支 as the classic vertical arrangement. Let me do 4 columns, each with the pillar name, gan, zhi stacked. ```html
年柱
甲
辰
...
``` That's clean. Let me do that. For 大运, a horizontal scroll of pillars: ```html
7岁
乙
卯
1991
...
``` Good. Let me write it all. Also add the 五行 color for each character. ```js const WX_COLOR = {木:'#3fbf6f', 火:'#e05a4e', 土:'#c89b3c', 金:'#d8d8d8', 水:'#4a90d9'}; ``` Hmm, for 金 maybe use a gold/silver. Let me use: 木 #4caf7d, 火 #e05252, 土 #c9a227, 金 #b0b7c3, 水 #4a8fd4. OK. Now writing the final code. One thing: the date input default. Let me set it to a fixed example or today. I'll set to today's date and current time. Let me write. Also, note the `toJDN` function: ```js function toJDN(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; } ``` Good. Now, let me write the complete file. Actually one more check on the month pillar for dates in early January. E.g., 2024-01-05, sunLon ≈ 284°? Let's see: Jan 5 is around 小寒 (285°). If sunLon = 284, then (284-315+360)%360 = 329, /30 = 10.96 → 10 → 子月. monthZhi = (2+10)%12 = 0 = 子 ✓. And the year would be 2023 (癸卯). monthGan = ((yearGan=9 for 癸)%5=4)*2+2+10 = 8+2+10 = 20 % 10 = 0 = 甲. So 甲子月. For 癸卯年, 子月 should be 甲子 (癸年 寅月=甲寅, then 子 is index 10: 甲+10 = 甲... 甲寅,乙卯,丙辰,丁巳,戊午,己未,庚申,辛酉,壬戌,癸亥,甲子 ✓). Now let me write everything. Let me be careful with `Date.UTC` for years < 100 — it maps to 1900+year. But our range is modern, fine. Writing the final answer now. I'll also handle input validation. Let me produce the code. Final structure of the JS: 1. Constants (GAN, ZHI, colors) 2. VSOP87 data 3. Astronomy functions 4. Calendar functions 5. BaZi functions 6. UI Let me write it. For the timezone select, options: -12..+14. Default +8. Let me write out. ```html ``` and populate with JS. OK, final code: Actually, let me reconsider the `lateZi` handling once more. Case: birth at 2024-02-10 23:30, lateZi = true (23:00 换日). - Day pillar uses 2024-02-11's 干支. - 2024-02-11 idx = (40+1) = 41 → 41%10=1 乙, 41%12=5 巳. 乙巳日. - Hour: hourZhi = 子 (0). hourGan = ((1%5)*2 + 0) % 10 = 2 = 丙. 丙子時. - For 乙日, 子時 = 丙子 ✓ (乙庚日 起丙子). Case: lateZi = false (00:00 换日). - Day pillar = 2024-02-10 = 甲辰日 (idx 40). - Hour 子時: hourGan = ((0%5)*2+0)%10 = 0 = 甲. 甲子時. (甲己日 起甲子 ✓) - So 晚子時 gets 甲子時 with the current day's stem. Both are reasonable interpretations. Good. Now writing. Let me write the CSS to be clean and elegant. I think I'm ready. Let me produce the final code. One last detail — in `render`, computing the Gregorian year for each 大运 start: ```js const startDate = new Date(birthMs + startAgeYears * 365.2425 * 86400000); ``` where birthMs is the UTC ms. Then display `startDate.getUTCFullYear()`. Actually for the local date, I should add the tz offset. Let me just use the year from birth year + startAge approximately, but better to compute properly: ```js const startMs = birthMs + ageYears*365.2425*86400000; const startYear = new Date(startMs + tz*3600000).getUTCFullYear(); ``` That gives the local calendar year. Good. Now writing the final code. I'll write it out completely. Let me make sure the `luck` array entries have their Gregorian years computed. ```js const luck = []; for(let k=1;k<=10;k++){ const age = startAgeYears + (k-1)*10; const idx = ((monthGZ + dir*k) % 60 + 60) % 60; const startMs = birthMs + age * 365.2425 * 86400000; const yr = new Date(startMs + tz*3600000).getUTCFullYear(); luck.push({ gan: idx%10, zhi: idx%12, age, year: yr }); } ``` Good. Now let me finalize. Also: should the 大运 be 10 or 12? Traditional shows 8-10. I'll do 10. Let me write it. Also I realize I should double check `ganzhiIndex`: ```js function ganzhiIndex(g, z){ for(let i=0;i<60;i++) if(i%10===g && i%12===z) return i; return 0; } ``` For g=0,z=2 (甲寅): i=2? 2%10=2 no. i=14: 14%10=4 no. i=26: 6 no. i=38: 8 no. i=50: 0 yes, 50%12=2 yes ✓. Returns 50. Good. OK, writing final. Let me write the complete HTML. Actually, I want to double-check the 五虎遁 formula with the month offset once more for a 己 year. 己年 (yearGan=5): monthGan for 寅 = ((5%5)*2 + 2 + 0) % 10 = (0+2)%10 = 2 = 丙 ✓ (甲己之年丙作首). And for 癸年 (9): ((9%5)*2+2) = (8+2) = 10 % 10 = 0 = 甲 ✓ (戊癸之年甲寅头). ✓ Great. Now writing. Let me write out the final code in full. I'll also add a footer note. Writing now. For robustness in `termJD`, the Newton iteration might overshoot for the initial guess if it's far. But the guesses are within a few days, and solar longitude changes ~1°/day, so diff is small. Fine. Hmm, one issue: the guess for 小寒 (285°) is Jan 6, and for 大雪 (255°) is Dec 7. These are fine. But note: `termJD(year, 285)` gives 小寒 of that Gregorian year (early January). Good. Let me write it. Final code: I'll write it now in the response. One more thought on performance: computing 12 terms × 3 years = 36 term computations, each with up to 40 Newton iterations, each evaluating the full VSOP series (~100 terms + ~50 terms). That's 36*40*150 ≈ 216,000 cos evaluations. That's fine, fast enough (< 100ms). Actually I can reduce iterations since it converges in ~3-4 iterations. Fine. OK, writing. ### assistant · answer This is a directly runnable HTML document that builds a BaZi (Four Pillars) chart, showing only the Four Pillars and Decennial Cycles (大运) with precise solar-term calculations. ```html 八字排盘 · 四柱与大运

八字排盘

FOUR PILLARS · DECENNIAL CYCLES
节气采用 VSOP87 天文算法实时推算
``` ### Charting Logic & Interaction The page turns a birth date and time into a clean four-pillar display and a scrollable Decennial Cycles list, with all calculations happening instantly in your browser. - **Four Pillars (四柱)**:Year, Month, Day, and Hour pillars are shown in separate cards, with the day pillar marked as 日元 (Day Master). - **Decennial Cycles (大运)**:Ten 10-year cycles are generated, each with starting age and Gregorian year, scrollable horizontally. - **Solar-Term Precision**:Uses the VSOP87 astronomical algorithm to compute solar longitude, determining year and month boundaries by the 24 solar terms. - **User Inputs**:Date, time, gender, timezone, and a “23:00 day change” option affect the chart; pressing Enter or clicking 排盘 recalculates immediately. The layout stays minimal and readable, with color-coded Heavenly Stems and Earthly Branches for quick visual scanning. --- **Optimization Tip:** You can adjust the number of Decennial Cycles by changing `k <= 10` in the `calcChart` function, and the supported year range (1900–2150) is set inside the `run` function if you need a wider span.