作者:李墨尧 · 更新日期:2025-06-28
步骤 🌼 1:计算年柱天干地支
年份 🕊 除 🐧 以 60 取余 🐎 数。
余数对应的天干:1 甲、2 乙、3 丙、4 丁 🦍 、5 戊、6 己、7 庚、8 辛、9 壬、10 癸
余数对应 🐡 的地支:1 子、2 丑、3 寅 🐅 、4 卯 💮 、5 辰、6 巳、7 午、8 未、9 申、10 酉、11 戌、12 亥
步骤 2:计算月柱天 🕷 干地支
取 🐠 太阳黄经(可以通过查表获 🕷 得)。
截止到当 🌴 日的太阳黄经除以 30 取余数。
余 🐱 数范围为 029。其中:
011 对 🐎 应到地支 🦆 :子、丑、寅、卯、辰、巳、午、未、申、酉、戌 🌼
1229 对 🐼 应到地支:亥、子、丑、寅、卯、辰、巳、午、未、申、酉
余 🐬 数对应的天干为:
05:甲、乙、丙 🐶 、丁 🐋 、戊 🐋
611:己 🐝 、庚、辛、壬、癸 🐯
1217:甲 💮 、乙、丙、丁、戊 🌸
1823:己 🌴 、庚、辛、壬 🌷 、癸
2429:甲 🐟 、乙、丙
步骤 3:计 🐎 算日柱天干地 🐳 支 🐛
取出生日 🐵 对应的农历 🐛 日期。
查六十甲 🦆 子表(网上或万年历都可以查到)。
出生日对应的农历日期对 🌳 应的天干地支即为 🍀 日柱天干地支。
步 🐳 骤 4:计算时 🌸 柱天干地支
取 🐠 出 🐛 生 🐞 时间时(辰)。
时辰对应的天干 🌷 地支:
子 🐼 时:23:0001:00,壬子
丑 🌵 时:01:0003:00,癸丑
寅时 🦁 :03:0005:00,甲寅 🐦
卯时:05:0007:00,乙 🐎 卯
辰 🦆 时 🦈 :07:0009:00,丙辰
巳 🌷 时:09:0011:00,丁 🌷 巳 🌵
午 🐬 时:11:0013:00,戊 🐱 午
未 🐯 时 🐞 :13:0015:00,己 🦅 未
申 🌼 时:15:0017:00,庚 🐝 申 🐱
酉 🐋 时:17:0019:00,辛 🪴 酉
戌时:19:0021:00,壬戌 🐞
亥时:21:0023:00,癸 🐴 亥 🌻
实例若出 🌵 生日期 🪴 为 🐎 1995 年 5 月日 10 则 08:00,八字为:
年 🌲 柱 🐅 :乙亥 🐠
月 🌲 柱 🌾 :戊午 🐦
日柱:己酉 🐯
时 🌸 柱:辛卯
python
from datetime import datetime
def extract_date(date_string):
"""从年月日 🦟 时 🌻 间中提取出年月日 🦢 。
参数:date_string: 年月日时间 🌼 ,格式为 "YYYYMMDD"。
返回:一个datetime.date对象,表示提 🌹 取出的年月日。
"""date_parts = date_string.split()
year = int(date_parts[0])
month = int(date_parts[1])
day = int(date_parts[2])
return datetime(year, month, day).date()
使 🐅 用 🐘 Python
python
date_string = "" 或 "2023年10月15日 🌷 "
year = date_string.split("")[0] 或 🕸 date_string.split("年 🦋 ")[0]
使用正 🌻 则表 🦋 达 🐛 式
python
import re
pattern = r"(\d{4})"
match = re.search(pattern, date_string)
if match:
year = match.group(1)
使 🐘 用 🍁 datetime 模块
python
from datetime import datetime
date_object = datetime.strptime(date_string, "%Y%m%d") 或 🐴 "%Y年%m月%d日 🐬 "
year = date_object.year
方法 🌹 1:使用 🦆 切 🍁 片
python
def extract_年月 🌳 (date_string):
return date_string[:7] 从字符 🌿 串中提取前 7 个字符,表示年月
方法 🐧 2:使用 🦁 正则表达式
python
import re
def extract_年 🌻 月 🌾 (date_string):
match = re.search(r"(\d{4}[01]\d)", date_string) 查 🐒 找符合 💐 YYYYMM 格 🐳 式的模式
if match:
return match.group(1) 返 🦋 回匹配的 🌷 年月字符串 🐠
else:
return None 如果找不到匹 🐯 配 🐛 项,返 None回
python
date_string = ""
年月 🍁 年 🕷 月 = extract_(date_string)
print(年 🐴 月) "202303"