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为闰月


公历转农历上面回调中已经实现过了ios - 农历公历互转 农历选择器_第1张图片

你可能感兴趣的:(ios - 农历公历互转 农历选择器)