iOS中根据生日计算星座♈️

来源:互联网 发布:怎么申请淘宝儿童模特 编辑:程序博客网 时间:2024/05/17 06:32

   这里的核心代码是转载别人的   不过四个方法加在一起很好用 亲自试验 效果嘎嘎叫

/**

 *  返回月份

 *

 *  @param date 时间

 *

 *  @return 月份

 */

-(int )returnMonth:(NSDate *)date

{

    //实例化一个NSDateFormatter对象

    

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    

    [dateFormatter setDateFormat:@"MM"];

    

    NSString *currentDateStr = [dateFormatter stringFromDate:date];

    

    return [currentDateStr intValue];

    

}


- (int )returnDay:(NSDate *)date

{

    //实例化一个NSDateFormatter对象

    

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    

    [dateFormatter setDateFormat:@"dd"];

    

    NSString *currentDateStr = [dateFormatter stringFromDate:date];

    

    return [currentDateStr intValue];

    

}


/**

 *  计算星座

 *

 *  @param m 月份

 *  @param d 日期

 *

 *  @return 结果

 */

-(NSString *)getAstroWithMonth:(int)m day:(int)d{

    

    NSString *astroString =@"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";

    

    NSString *astroFormat = @"102123444543";

    

    NSString *result;

    

    if (m<1||m>12||d<1||d>31){

        

        return @"错误日期格式!";

        

    }

    

    if(m ==2 && d>29)//2月份

        

    {

        return@"错误日期格式!!";

        

    }else if(m==4 || m==6 || m==9 || m==11) {

        

        if (d>30) {

            

            return @"错误日期格式!!!";

            

        }

        

    }

    

    result = [NSString stringWithFormat:@"%@",[astroStringsubstringWithRange:NSMakeRange(m *2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1),1)] intValue] - (-19)) *2,2)]];

    

    return result;

    

}


/**

 *  获得星座的方法

 *

 *  @return 星座

 */

- (NSString*)getconstellation{

    

    NSString *birthStr = self.infoDict[@"birthday"];

    

    NSDateFormatter* dateFormat = [[NSDateFormatteralloc] init];//实例化一个NSDateFormatter对象

    [dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式,要注意跟下面的dateString匹配,否则日起将无效

    NSDate *selectdate =[dateFormat dateFromString:birthStr];

    

    NSString *xingzuoStr = [selfgetAstroWithMonth:[selfreturnMonth:selectdate] day:[self returnDay:selectdate]];

    

    return xingzuoStr;

    

}




0 0
原创粉丝点击