八字 = 年柱 + 月柱 + 日柱 + 时柱
通俗说八字,其实就是古代的天干地支记时法。十天干十二地支的组合,记录时间。
60甲子为记时一个循环,也就是60年一轮回
_date_jiazi = @[
@"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉",
@"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛巳", @"壬午", @"癸未",
@"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳",
@"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸卯",
@"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑",
@"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥"
];
#十二地支
dz_num = @[@"子", @"丑", @"寅", @"卯", @"辰", @"巳",
@"午", @"未", @"申", @"酉", @"戌", @"亥" ];
#十天干
_tg_num = @[@"甲", @"乙", @"丙" , @"丁" ,@"戊" ,
@"己", @"庚", @"辛", @"壬", @"癸"];
获取公历日期 转农历日期
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth
| NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
#今日时间dateCom
NSDateComponents *dateComponent = [calendar components:unitFlags
fromDate:[NSDate date]];
_dateCom = dateComponent;
# 获取特定时间的date
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-M-d"];
NSString * timeStr = [NSString stringWithFormat:@"%ld-%ld-%ld",dateCom.year,dateCom.month,dateCom.day];
NSDate* dateTemp = [formater dateFromString:timeStr];
#农历时间dateCom
NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents* NongLiCom = [localeCalendar components:unitFlags
fromDate:dateTemp];
#NongLiCom.year = 甲子数【1-60】
1,年柱
NSMutableDictionary * dateDic = [NSMutableDictionary dictionary];
NSInteger idx = self.NongLiCom.year - 1;
NSString* y = self.date_jiazi[idx];
NSString *y_tg = [y substringToIndex:1];
NSString *y_dz = [y substringFromIndex:1];
NSDictionary *yearDic = @{
@"tgdz":y, //年柱
@"tg":y_tg, //年地支
@"dz":y_dz //年地支
};
2 月柱
甲己之年丙作首,乙庚之岁戊为头;
丙辛必定寻庚起,丁壬壬位顺行流;
更有戊癸何方觅,甲寅之上好追求。
写完发现与百度的天干地支对不上。经查发现原来与节气有关:
如2017-7 农历6月初四为乙庚午月 6月初五为辛未月
月柱月份
1月 | 2月 | 3月 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|
小寒-立春 | 立春-惊蛰 | 惊蛰-清明 | 清明-立夏 | 立夏-芒种 | 芒种-小暑 | 小暑-立秋 | 立秋-白露 | 白露-寒露 | 寒露-立冬 | 立冬-大雪 | 大雪-小寒 |
1月:小寒-立春 2月:立春-惊蛰 3月 :惊蛰-清明 4月:清明-立夏
_date_month =@{
@"甲、己" : @[@"丙寅" , @"丁卯" , @"戊辰" , @"己巳" , @"庚午" , @"辛未" , @"壬申" , @"癸酉" , @"甲戌" , @"乙亥" , @"丙子" , @"丁丑" ],
@"乙、庚" : @[@"戊寅" , @"己卯" , @"庚辰" , @"辛巳" , @"壬午" , @"癸未" , @"甲申" , @"乙酉" , @"丙戌" , @"丁亥" , @"戊子" , @"己丑" ],
@"丙、辛" : @[@"庚寅" , @"辛卯" , @"壬辰" , @"癸巳" , @"甲午" , @"乙未" , @"丙申" , @"丁酉" , @"戊戌" , @"己亥" , @"庚子" , @"辛丑" ],
@"丁、壬" : @[@"壬寅" , @"癸卯" , @"甲辰" , @"乙巳" , @"丙午" , @"丁未" , @"戊申" , @"己酉" , @"庚戌" , @"辛亥" , @"壬子" , @"癸丑" ],
@"戊、癸" : @[@"甲寅" , @"乙卯" , @"丙辰" , @"丁巳" , @"戊午" , @"己未" , @"庚申" , @"辛酉" , @"壬戌" , @"癸亥" , @"甲子" , @"乙丑" ]
};
NSArray * month_arry = [NSArray array];
if ([@"甲、己" containsString: y_tg]) {
month_arry = self.date_month[@"甲、己"];
}else if ([@"乙、庚" containsString:y_tg]){
month_arry = self.date_month[@"乙、庚"];
}else if ([@"丙、辛" containsString: y_tg]){
month_arry = self.date_month[@"丙、辛"];
}else if ([@"丁、壬" containsString: y_tg]){
month_arry = self.date_month[@"丁、壬"];
}else if ([@"戊、癸" containsString: y_tg]){
month_arry = self.date_month[@"戊、癸"];
}
#计算节气月的起始天
int index = [self solaComput:self.NongLiCom.month] - 1;
index = index<0?11:index;
NSString *m = month_arry[index];
NSString* m_tg = [m substringToIndex:1];
NSString *m_dz = [m substringFromIndex:1];![图片.png](https://upload-images.jianshu.io/upload_images/7802419-247ce19e1be83c97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
NSDictionary *monDic = @{
@"tgdz":m,
@"tg":m_tg,
@"dz":m_dz
};
[dateDic setObject:monDic forKey:@"month"];
#计算节气
-(int)solaComput:(NSInteger)month
{
int year_num = self.GongLiCom.year % 100;
BOOL C = (self.GongLiCom.year/100 == 19) ;
int newMonth = (int)month;
switch (self.NongLiCom.month) {
case 1:{//小寒
int aa = (int)(year_num * 0.2422 + (C?6.11 : 5.4055) - (year_num-1)/4 );
if(self.GongLiCom.year == 2019) {
aa -= 1;
}else if (self.GongLiCom.year == 1918){
aa += 1;
}
if (month < aa) {
newMonth = 0;
}
NSLog(@"\n----小寒: 1月%d", aa);
}
break;
case 2:{//立春
int aa = (int)(year_num * 0.2422 + (C?4.15 : 3.87) - (year_num-1)/4 );
if (month < aa) {
newMonth = 1;
}
NSLog(@"\n----立春: 2月%d", aa);
}
break;
case 3:{//惊蛰
int aa = (int)(year_num * 0.2422 + 5.63 - (year_num)/4 );
if (month < aa) {
newMonth = 2;
}
NSLog(@"\n----惊蛰: 3月%d", aa);
}
break;
case 4:{//清明
int aa = (int)(year_num * 0.2422 + (C?5.59 : 4.81) - (year_num)/4 );
if (self.GongLiCom.year == 1911) {
aa += 1;
}
if (month < aa) {
newMonth = 3;
}
NSLog(@"\n----清明: 4月%d", aa);
}
break;
case 5:{//立夏
int aa = (int)(year_num * 0.2422 + (C?6.318 : 5.52) - (year_num)/4 );
if (month < aa) {
newMonth = 4;
}
NSLog(@"\n----立夏: 5月%d", aa);
}
break;
case 6:{//芒种
int aa = (int)(year_num * 0.2422 + (C?6.5 : 5.678) - (year_num)/4 );
if (self.GongLiCom.year == 1902) {
aa +=1;
}
if (month < aa) {
newMonth = 5;
}
NSLog(@"\n----芒种: 6月%d", aa);
}
break;
case 7:{//小暑
int aa = (int)(year_num * 0.2422 + (C?7.928 : 7.108) - (year_num)/4 );
if (self.GongLiCom.year == 1925 || self.GongLiCom.year == 2016) {
aa +=1;
}
if (month < aa) {
newMonth = 6;
}
NSLog(@"\n----小暑: 7月%d",aa);
}
break;
case 8:{//立秋
int aa = (int)(year_num * 0.2422 + (C?8.44 : 7.5) - (year_num)/4 );
if (self.GongLiCom.year == 2002) {
aa +=1;
}
if (month < aa) {
newMonth = 7;
}
NSLog(@"\n----立秋: 8月%d", aa);
}
break;
case 9:{//白露
int aa = (int)(year_num * 0.2422 + (C?8.44 : 7.646) - (year_num)/4 );
if (self.GongLiCom.year == 1927) {
aa +=1;
}
if (month < aa) {
newMonth = 8;
}
NSLog(@"\n----白露: 9月%d", aa);
}
break;
case 10:{//寒露
int aa = (int)(year_num * 0.2422 + (C?9.098 : 8.318) - (year_num)/4 );
if (month < aa) {
newMonth = 9;
}
NSLog(@"\n----寒露: 10月%d", aa);
}
break;
case 11:{//立冬
int aa = (int)(year_num * 0.2422 + (C?8.218 : 7.438) - (year_num)/4 );
if (self.GongLiCom.year == 2089) {
aa +=1;
}
if (month < aa) {
newMonth = 10;
}
NSLog(@"\n----立冬: 11月%d", aa);
}
break;
case 12:{//大雪
int aa = (int)(year_num * 0.2422 + (C?7.9 : 7.18) - (year_num)/4 );
if (self.GongLiCom.year == 1954) {
aa +=1;
}
if (month < aa) {
newMonth = 11;
}
NSLog(@"\n----大雪: 12月%d", aa);
}
break;
default:
break;
}
}
3,日柱
-(NSDictionary*)getDayTgDz:(NSDateComponents*)datecom
{
//日
// G = 4C + [C / 4] + 5y + [y / 4] + [3 * (M + 1) / 5] + d - 3
// Z = 8C + [C / 4] + 5y + [y / 4] + [3 * (M + 1) / 5] + d + 7 + I
// 其中 C 是世纪数减一,y 是年份后两位,M 是月份,d 是日数。1月和2月按上一年的13月和14月来算。奇数月i=0,偶数月i=6。G 除以10的余数是天干,Z 除以12的余数是地支
int C_num = (int)datecom.year/100;
int Y_num = datecom.year % 100;
int M_num ;
if (datecom.month == 1) {
M_num = 13;
Y_num -=1;
}else if(datecom.month == 2){
M_num = 14;
Y_num -=1;
}else{
M_num = (int)datecom.month;
}
int js = (datecom.month%2) ? 0 : 6;
int G = 4*C_num + (C_num / 4) + 5*Y_num + (Y_num / 4) + (3 * (M_num + 1) / 5) + (int)datecom.day - 3 -1;
int Z = 8*C_num + (C_num / 4) + 5*Y_num + (Y_num/ 4) + (3 * (M_num + 1) / 5) + (int)datecom.day +7 + js -1;
NSString *d_tg = self.tg_num[G%10];
NSString *d_dz = self.dz_num[Z%12];
NSString *d = [d_tg stringByAppendingString:d_dz]; ;
return @{
@"tgdz":d,
@"tg":d_tg,
@"dz":d_dz
};
}
4,时柱
/**
* 日上起时
* 甲己还生甲,乙庚丙作初,
* 丙辛从戊起,丁壬庚子居,
* 戊癸何方发,壬子是真途。
*/
int h_inx = ([self.hour intValue]+1)/ 2;
h_inx = h_inx>11?0:h_inx;
NSString * h_dz = self.dz_num[h_inx];
//日天干
NSString *d_tg = dayDic[@"tg"];
NSString *h_tg = @"";
if ([@"甲己" containsString:d_tg]) {
int tg_indx = h_inx % 10;
h_tg = self.tg_num[tg_indx];
}else if([@"乙庚"containsString:d_tg]){
int tg_indx = (h_inx +2) % 10;
h_tg = self.tg_num[tg_indx];
}else if([@"丙辛"containsString:d_tg]){
int tg_indx = (h_inx +4) % 10;
h_tg = self.tg_num[tg_indx];
}else if([@"丁壬"containsString:d_tg]){
int tg_indx = (h_inx +6) % 10;
h_tg = self.tg_num[tg_indx];
}else if([@"戊癸"containsString:d_tg]){
int tg_indx = (h_inx +8) % 10;
h_tg = self.tg_num[tg_indx];
}
NSString *h = [h_tg stringByAppendingString:h_dz];
NSDictionary *hourDic = @{
@"tgdz":h,
@"tg":h_tg,
@"dz":h_dz
};
[dateDic setObject:hourDic forKey:@"hour"];
百度弄了一天终于搞出来,传统文化博大精深啊,天干地支和很多东西都挂钩(如五行、生肖、老黄历中的东西基本都是用八字推算的)。
到这里八字就算完了 ,计算生辰八字 只需带入出生那年的年月日时即可。
原创感觉很棒