博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
判断NSDate是星期几和农历
阅读量:5883 次
发布时间:2019-06-19

本文共 4551 字,大约阅读时间需要 15 分钟。

  hot3.png

判断NSDate是星期几和农历

http://www.jianshu.com/p/5850a5d03b8f

UIDatePicker使用技巧

1、怎么改frame的宽高?

UIDatePicker的三个高度,162、180、216

UIDatePicker*_datePicker = [[UIDatePicker alloc]init];_datePicker.frame = CGRectMake(20, 100, 280, 162);

2、判断NSDate是星期几

- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期天", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];    [calendar setTimeZone: timeZone];    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];    return [weekdays objectAtIndex:theComponents.weekday];}

3、判断NSDate的农历

-(NSString*)getChineseCalendarWithDate:(NSDate *)date{    NSArray *chineseYears = [NSArray arrayWithObjects:                             @"甲子", @"乙丑", @"丙寅", @"丁卯",  @"戊辰",  @"己巳",  @"庚午",  @"辛未",  @"壬申",  @"癸酉",                             @"甲戌",   @"乙亥",  @"丙子",  @"丁丑", @"戊寅",   @"己卯",  @"庚辰",  @"辛己",  @"壬午",  @"癸未",                             @"甲申",   @"乙酉",  @"丙戌",  @"丁亥",  @"戊子",  @"己丑",  @"庚寅",  @"辛卯",  @"壬辰",  @"癸巳",                             @"甲午",   @"乙未",  @"丙申",  @"丁酉",  @"戊戌",  @"己亥",  @"庚子",  @"辛丑",  @"壬寅",  @"癸丑",                             @"甲辰",   @"乙巳",  @"丙午",  @"丁未",  @"戊申",  @"己酉",  @"庚戌",  @"辛亥",  @"壬子",  @"癸丑",                             @"甲寅",   @"乙卯",  @"丙辰",  @"丁巳",  @"戊午",  @"己未",  @"庚申",  @"辛酉",  @"壬戌",  @"癸亥", nil];    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];    NSLog(@"%zd_%zd_%zd",localeComp.year,localeComp.month,localeComp.day);    NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];    NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];    NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];    NSString *chineseCal_str =[NSString stringWithFormat: @"%@_%@_%@",y_str,m_str,d_str];    return chineseCal_str;  }

 

//农历- (void)setDate:(NSDate *)date{    static NSArray *dayArray;    static NSArray *monthArray;    static dispatch_once_t once;        dispatch_once(&once, ^{    dayArray  = @[ @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十"];                monthArray = @[@"正月",@"二月",@"三月",@"四月",@"五月",@"六月",@"七月",@"八月",@"九月",@"十月",@"冬月",@"腊月"];    });                static NSDateFormatter *dateFormatter;    if(!dateFormatter){        dateFormatter = [NSDateFormatter new];        dateFormatter.timeZone = self.calendarManager.calendarAppearance.calendar.timeZone;        [dateFormatter setDateFormat:@"dd"];    }        _date = date;        textLabel.text = [dateFormatter stringFromDate:date];     //获取农历#ifdef __IPHONE_8_0        NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];        unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;        NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];#else    NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];        unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;        NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];#endif      lunarTextLabel.text = dayArray[localeComp.day-1];    if (localeComp.day-1 == 0) {        lunarTextLabel.text = monthArray[localeComp.month-1];    }        isToday = -1;    cacheCurrentDateText = nil;}

 

转载于:https://my.oschina.net/huqiji/blog/804846

你可能感兴趣的文章
CentOS 5 全功能WWW服务器搭建全教程
查看>>
scala111
查看>>
模块化服务规范——OSGI
查看>>
劣质代码评析——猜数字问题(上)
查看>>
纸上谈兵: 栈 (stack)
查看>>
Windows phone8 基础篇(三) 常用控件开发
查看>>
Oracle学习笔记之五,Oracle 11g的PL/SQL入门
查看>>
大叔手记(3):Windows Silverlight/Phone7/Mango开发学习系列教程
查看>>
考拉消息中心消息盒子处理重构(策略模式)
查看>>
so easy 前端实现多语言
查看>>
【追光者系列】HikariCP源码分析之ConcurrentBag&J.U.C SynchronousQueue、CopyOnWriteArrayList...
查看>>
canvas系列教程05-柱状图项目3
查看>>
css绘制几何图形
查看>>
HTML标签
查看>>
理解JS中的Event Loop机制
查看>>
转载:字符编码笔记:ASCII,Unicode和UTF 8
查看>>
修复看不懂的 Console Log
查看>>
Android跨进程通信 AIDL使用
查看>>
ajax常见面试题
查看>>
结合kmp算法的匹配动画浅析其基本思想
查看>>