纸上得来终觉浅,绝知此事要躬行。

0%

根据NSDate得到农历的年份(包括生肖)、月、日

网络上的很多得到农历的年份都是错误的。再次更正一下:

关于农历的年份

六十甲子又称六十花甲子,是汉族人民最早、最大的发明创造,其最古老的用途是纪年、纪月、纪日、纪时。纪年为60年一个周期,纪月为5年一个周期,纪日为60天一个周期,纪时为5天一个周期。

以天干和地支按顺序相配﹐即甲、乙、丙、丁、戊、己、庚、辛、壬、癸与子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥相组合,从”甲子”起﹐到”癸亥”止﹐满六十为一周﹐称为”六十甲子”。亦称”六十花甲子”。 又因起头是“甲”字的有六组,所以也叫“六甲”。

关于12生肖

十二生肖是十二地支的形象化代表,即子(鼠)、丑(牛)、寅(虎)、卯(兔)、辰(龙)、巳(蛇)、午(马)、未(羊)、申(猴)、酉(鸡)、戌(狗)、亥(猪),随着历史的发展逐渐融合到相生相克的民间信仰观念,表现在婚姻、人生、年运等,每一种生肖都有丰富的传说,并以此形成一种观念阐释系统,成为民间文化中的形象哲学,如婚配上的属相、庙会祈祷、本命年等。现代,更多人把生肖作为春节的吉祥物,成为娱乐文化活动的象征。

农历的年份 (生肖) 例如:甲子(鼠)年

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
+ (NSString *)getChineseYearWithDate:(NSDate *)date{
NSArray *chineseYears = [NSArray arrayWithObjects:
@"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉",
@"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛巳", @"壬午", @"癸未",
@"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳",
@"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸卯",
@"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑",
@"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥", nil];

NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];

unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;

NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];


NSString *y_str = [chineseYears objectAtIndex:localeComp.year - 1];

NSString *Cz_str = nil;
if ([y_str hasSuffix:@"子"]) {
Cz_str = @"鼠";
}else if ([y_str hasSuffix:@"丑"]){
Cz_str = @"牛";
}else if ([y_str hasSuffix:@"寅"]){
Cz_str = @"虎";
}else if ([y_str hasSuffix:@"卯"]){
Cz_str = @"兔";
}else if ([y_str hasSuffix:@"辰"]){
Cz_str = @"龙";
}else if ([y_str hasSuffix:@"巳"]){
Cz_str = @"蛇";
}else if ([y_str hasSuffix:@"午"]){
Cz_str = @"马";
}else if ([y_str hasSuffix:@"未"]){
Cz_str = @"羊";
}else if ([y_str hasSuffix:@"申"]){
Cz_str = @"猴";
}else if ([y_str hasSuffix:@"酉"]){
Cz_str = @"鸡";
}else if ([y_str hasSuffix:@"戌"]){
Cz_str = @"狗";
}else if ([y_str hasSuffix:@"亥"]){
Cz_str = @"猪";
}

return [NSString stringWithFormat:@"%@(%@)年",y_str,Cz_str];
}

农历的月、日

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
+ (NSString *)getChineseCalendarWithDate:(NSDate *)date{
NSArray *chineseMonths=[NSArray arrayWithObjects:
@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
@"九月", @"十月", @"冬月", @"腊月", nil];

NSArray *chineseDays=[NSArray arrayWithObjects:
@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];


NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];

unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;

NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];

NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];

NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];

NSString *chineseCal_str =[NSString stringWithFormat: @"%@%@",m_str,d_str];

return chineseCal_str;
}

今天是周几?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
+ (NSString *)getWeekDayWithDate:(NSDate *)date{
//只适用于iOS8
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 通过已定义的日历对象,获取某个时间点的NSDateComponents表示,并设置需要表示哪些信息(NSYearCalendarUnit, NSMonthCalendarUnit, NSDayCalendarUnit等)

NSDateComponents *dateComponents = [greCalendar components:NSCalendarUnitWeekday fromDate:date];
NSString *str = [NSString stringWithFormat:@"%li",(long)dateComponents.weekday];

if ([str isEqual: @"1"]) {
return @"周日";
}else if ([str isEqual:@"2"]){
return @"周一";
}else if ([str isEqual:@"3"]){
return @"周二";
}else if ([str isEqual:@"4"]){
return @"周三";
}else if ([str isEqual:@"5"]){
return @"周四";
}else if ([str isEqual:@"6"]){
return @"周五";
}else{
return @"周六";
}

}

本年或者本月的第几周 第一个参数传YES 得到本年的第几周 NO为本月的第几周。

1
2
3
4
5
6
7
8
9
10
11
12
+ (NSString *)getWeekInyearOrMouth:(BOOL)inYear WithDate:(NSDate *)date{
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 通过已定义的日历对象,获取某个时间点的NSDateComponents表示,并设置需要表示哪些信息(NSYearCalendarUnit, NSMonthCalendarUnit, NSDayCalendarUnit等)

NSDateComponents *dateComponents = [greCalendar components:NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth fromDate:date];
if (inYear) {
return [NSString stringWithFormat:@"第%li周",(long)dateComponents.weekOfYear];
}else{
return [NSString stringWithFormat:@"第%li周",(long)dateComponents.weekOfMonth];
}

}