用戶註冊/資料完善頁常需根據出生日期自動推算星座與生肖。本文提供健壯、可維護、支持國際化擴展的完整實現方案,修正原文邏輯漏洞(如月份索引偏移、閏年邊界),並對比多種實現策略優劣。
const ZODIAC_RANGES = [
{ sign: '摩羯座', start: [1, 20], end: [1, 19] }, // 跨年特殊處理
{ sign: '水瓶座', start: [1, 20], end: [2, 18] },
{ sign: '雙魚座', start: [2, 19], end: [3, 20] },
{ sign: '白羊座', start: [3, 21], end: [4, 19] },
{ sign: '金牛座', start: [4, 20], end: [5, 20] },
{ sign: '雙子座', start: [5, 21], end: [6, 21] },
{ sign: '巨蟹座', start: [6, 22], end: [7, 22] },
{ sign: '獅子座', start: [7, 23], end: [8, 22] },
{ sign: '室女座', start: [8, 23], end: [9, 22] },
{ sign: '天秤座', start: [9, 23], end: [10, 22] },
{ sign: '天蠍座', start: [10, 23], end: [11, 21] },
{ sign: '人馬座', start: [11, 22], end: [12, 21] },
{ sign: '摩羯座', start: [12, 22], end: [12, 31] } // 年末段
];
(基準年 - 出生年) % 12 映射到 12 生肖陣列% 對負數行為與數學定義不同,需修正/**
* 計算星座
* @param {number} month 1-12
* @param {number} day 1-31
* @returns {string} 星座名稱
*/
function getZodiacSign(month, day) {
// 參數校驗
if (month < 1 || month > 12 || day < 1 || day > 31) return '未知';
// 將日期轉為「月日」數字便於比較:1月20日 → 120
const md = month * 100 + day;
// 星座邊界數據(月日數字)
const boundaries = [
{ sign: '摩羯座', start: 120, end: 119 }, // 特殊:跨年
{ sign: '水瓶座', start: 120, end: 218 },
{ sign: '雙魚座', start: 219, end: 320 },
{ sign: '白羊座', start: 321, end: 419 },
{ sign: '金牛座', start: 420, end: 520 },
{ sign: '雙子座', start: 521, end: 621 },
{ sign: '巨蟹座', start: 622, end: 722 },
{ sign: '獅子座', start: 723, end: 822 },
{ sign: '室女座', start: 823, end: 922 },
{ sign: '天秤座', start: 923, end: 1022 },
{ sign: '天蠍座', start: 1023, end: 1121 },
{ sign: '人馬座', start: 1122, end: 1221 },
{ sign: '摩羯座', start: 1222, end: 1231 }
];
for (const { sign, start, end } of boundaries) {
if (start <= end) {
// 正常區間
if (md >= start && md <= end) return sign;
} else {
// 跨年區間(摩羯座)
if (md >= start || md <= end) return sign;
}
}
return '未知';
}
/**
* 計算生肖
* @param {number} year 出生年份 (如 1990)
* @returns {string} 生肖名稱
*/
function getChineseZodiac(year) {
if (year < 1900 || year > 2100) return '未知';
const zodiacs = ['鼠', '牛', '虎', '兔', '龍', '蛇', '馬', '羊', '猴', '雞', '狗', '豬'];
// 1900 年是庚子年(鼠),索引 0
const baseYear = 1900;
let index = (year - baseYear) % 12;
// JS % 負數修正
if (index < 0) index += 12;
return zodiacs[index];
}
// 組合導出
function getAstroInfo(year, month, day) {
return {
zodiac: getZodiacSign(month, day),
chineseZodiac: getChineseZodiac(year)
};
}
// 測試
console.log(getAstroInfo(1990, 5, 15)); // {zodiac: "金牛座", chineseZodiac: "馬"}
console.log(getAstroInfo(2000, 1, 10)); // {zodiac: "摩羯座", chineseZodiac: "龍"}
month = document.zodiac.month.selectedIndex 返回 0-11,但邏輯判斷用 1-12 → 全部偏移一位if 重複,難維護、易漏寫(如 2 月 29 日閏年)(start - birthyear) % 12 邏輯反向且負數處理缺失document.zodiac 表單引用,不符合現代 DOM 標準<form id="astro-form">
<div>
<label>出生年份: <input type="number" name="year" min="1900" max="2100" required></label>
</div>
<div>
<label>出生月份:
<select name="month" required>
<option value="">請選擇</option>
<script>
for(let m=1; m<=12; m++)
document.write(`<option value="${m}">${m}月</option>`);
</script>
</select>
</label>
</div>
<div>
<label>出生日期: <input type="number" name="day" min="1" max="31" required></label>
</div>
<button type="button" id="calc-btn">計算星座/生肖</button>
<div>星座: <output id="zodiac-out"></output></div>
<div>生肖: <output id="cz-out"></output></div>
</form>
<script>
document.getElementById('calc-btn').addEventListener('click', () => {
const form = document.getElementById('astro-form');
const year = +form.year.value;
const month = +form.month.value;
const day = +form.day.value;
if (!year || !month || !day) return alert('請填寫完整日期');
const {zodiac, chineseZodiac} = getAstroInfo(year, month, day);
document.getElementById('zodiac-out').textContent = zodiac;
document.getElementById('cz-out').textContent = chineseZodiac;
});
</script>
若需根據農曆日期計算生肖,需引入農曆轉換庫(如 lunar-javascript):
import Lunar from 'lunar-javascript';
function getChineseZodiacByLunar(lunarYear, lunarMonth, lunarDay) {
const lunar = Lunar.fromYmd(lunarYear, lunarMonth, lunarDay);
return lunar.getYearZodiac(); // 直接返回生肖
}
if-else 鏈(year - 1900) % 12 映射 12 生肖陣列,修正負數取模將這套邏輯封裝為 astro-utils.js 納入組件庫,下次需求來臨直接 import { getAstroInfo } from '@/utils/astro',專業又高效。