iOS 根據生日得到生肖,星座,年齡的演算法

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   sp   for   

根據使用者生日,得到相應的年齡,星座和生肖。有的項目中可能會用到,貼出來共用。

得到年齡,這個很簡單了:

- (void)getAgeWith:(NSDate*)birthday{    //日曆    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];        NSUInteger unitFlags = NSCalendarUnitYear;        NSDateComponents *components = [gregorian components:unitFlags fromDate:birthday toDate:[NSDate  date] options:0];    self.age.text = [NSString stringWithFormat:@"%ld歲了",[components year]+1];}

根據月日得到星座:

//得到星座的演算法-(NSString *)getAstroWithMonth:(NSInteger)m day:(NSInteger)d{        NSString *astroString = @"魔羯水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍射手魔羯";        NSString *astroFormat = @"102123444543";        NSString *result;        if (m<1||m>12||d<1||d>31){                return @"錯誤日期格式!";            }        if(m==2 && d>29)            {                return @"錯誤日期格式!!";            }else if(m==4 || m==6 || m==9 || m==11) {                if (d>30) {                        return @"錯誤日期格式!!!";                    }            }        result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];        return [result stringByAppendingString:@"座"];    }

根據年份得到生肖:

-(NSString *)getZodiacWithYear:(NSInteger)y{    if (y <0) {        return @"錯誤日期格式!!!";    }        NSString *zodiacString = @"鼠牛虎兔龍蛇馬羊猴雞狗豬";        NSRange range = NSMakeRange ((y+9)%12-1, 1);        NSString*  result = [zodiacString  substringWithRange:range];        return [result stringByAppendingString:@"年"];    }

寫了一個demo,需要的去這裡下載:https://github.com/wangdachui/WTZodiacSigns

iOS 根據生日得到生肖,星座,年齡的演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.