300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > ios - 农历公历互转 农历选择器

ios - 农历公历互转 农历选择器

时间:2021-01-21 02:59:38

相关推荐

ios - 农历公历互转 农历选择器

通过UIpickerView实现date(农历)选择器(类似UIdatePicker),可以实现生日提醒等功能

1,需要导入

#import "solarOrLunar.h"#import "solarActionView.h"#import "Date+string.h"

源码下载链接:SolarOrLunarDeom

在需要实现的函数中添加

例如:

点击按钮

/*选择农历*//**************************************/- (void)optionDate{/*弹出农历时间选择器*/solarActionView *actionView =[[solarActionView alloc] initWithViewdelegate:self WithSheetTitle:@"农历" sheetMode:type_lunar];// [actionView initWithDate:[NSDate date]];//设定初始时间[actionView showInView:self.view];}

对应的回调方法

#pragma mark - ActionSheetViewDetaSource- (void)actionSheetSolarDate:(NSString *)year Month:(NSString *)month Day:(NSString *)day reserved:(NSString *)reserved{/*reserved=1为闰月*/NSLog(@"lunar:%@-%@-%@-%@",year,month,day,reserved);int year_ =[year intValue];int month_ =[month intValue];int day_ =[day intValue];int reserved_ =[reserved intValue];hjz solar =lunar_to_solar(year_, month_, day_, reserved_);NSLog(@"solar: %d-%d-%d\n",solar.year,solar.month,solar.day);NSString *dateAndTime =[NSString stringWithFormat:@"%d/%d/%d 08:00",solar.year,solar.month,solar.day];NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];NSDate *date =[dateFormatter dateFromString:dateAndTime];/*得到转换过来了公历NSDate数据*//*检验时间是否正确dateAndTime = [dateFormatter stringFromDate:date];//得到字符串 选择的日期间NSLog(@"转换后的公历:%@",dateAndTime);*//*对应的天干、地支、属相*/if(year_>1921)NSLog(@"属相日期:%@",[Date_string LunarForSolar:date]);}- (void)actionSheetTitleDateText:(NSString *)year Month:(NSString *)month day:(NSString *)day{NSLog(@"农历日期:%@年%@月%@日",year,month,day);}

农历转公历

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];[formatter setDateFormat:@"yyyy-MM-dd"];NSString *Date = [formatter stringFromDate:[NSDate date]];NSArray *arr=[Date componentsSeparatedByString:@"-"];int year =[arr[0] intValue];int month =[arr[1] intValue];int day =[arr[2] intValue];hjz lunar =solar_to_lunar(year, month, day);//将当前的公历时间转换为农历printf("lunar:=>%d-%d-%d-%d",lunar.year,lunar.month,lunar.day,lunar.reserved);//reserved=1为闰月

公历转农历上面回调中已经实现过了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。