您的位置:首页 > 其它

日历 NSCalendar

2016-01-08 00:00 267 查看
摘要: 这份代码很简单介绍而来NSCalendar的大概用法,界面只有sb拖出来的button,点击会打印相应的日期。
#import "ViewController.h"@interface  ViewController ()@property (strong,nonatomic) NSCalendar *calender1;@property (strong,nonatomic) NSCalendar *calender2;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (IBAction)tapBtn1:(id)sender{self.calender1 = [NSCalendar currentCalendar];self.calender2 = [NSCalendar autoupdatingCurrentCalendar];NSLog(@"%@",self.calender1.calendarIdentifier);NSLog(@"%@",self.calender2.calendarIdentifier);}- (IBAction)tapBtn2:(id)sender{NSLog(@"%@",self.calender1.calendarIdentifier);NSLog(@"%@",self.calender2.calendarIdentifier);}//根据提供的日历标示符初始化- (IBAction)tapBtn3:(id)sender{NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];NSLog(@"%@",calendar.calendarIdentifier);}//设置本地化信息- (IBAction)tapBtn4:(id)sender{NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];[calendar setLocale:locale];NSLog(@"%@",calendar.locale.localeIdentifier);}//设置时区信息- (IBAction)tapBtn5:(id)sender{NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"HKT"]];NSLog(@"%@",calendar.timeZone);}//设置每周的第一天从星期几开始- (IBAction)tapBtn6:(id)sender{NSCalendar *calendar = [NSCalendar currentCalendar];[calendar setFirstWeekday:3];NSLog(@"%lu",(unsigned long)calendar.firstWeekday);}//NSCalendar对象的 - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date//取得一个NSDate对象的1个或多个部分,用NSDateComponents来封装//需要注意的是,只有明确指定了unitFlags,NSDateComponents相应的那一部分才有值。- (IBAction)tapBtn7:(id)sender{NSCalendar *calendar = [NSCalendar currentCalendar];NSDate *date = [NSDate date];NSDateComponents *compt = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date];NSLog(@"%ld,%@",(long)[compt year],date);NSLog(@"%ld,%@",(long)[compt month],date);NSLog(@"%ld,%@",(long)[compt day],date);}//NSCalendar对象的 - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options : (NSUInteger)opts////取得两个NSDate对象的时间间隔,用NSDateComponents来封装- (IBAction)tapBtn8:(id)sender{NSCalendar *calendar = [NSCalendar currentCalendar];NSDate *date = [NSDate date];NSDate *date2 = [NSDate dateWithTimeInterval:5*60*60+75 sinceDate:date];NSDateComponents *compt = [calendar components:(NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date toDate:date2 options:0];NSLog(@"%ld",(long)[compt minute]);NSLog(@"%ld",(long)[compt second]);}//NSCalendar对象的 - (NSDate *)dateFromComponents:(NSDateComponents *)comps//根据NSDateComponents对象得到一个NSDate对象- (IBAction)tapBtn9:(id)sender{NSDateComponents *compt = [[NSDateComponents alloc] init];[compt setYear:2012];[compt setMonth:5];[compt setDay:11];NSCalendar *calendar = [NSCalendar currentCalendar];NSDate *date = [calendar dateFromComponents:compt];//得到本地时间,避免时区问题//获取当前时区NSTimeZone *zone = [NSTimeZone systemTimeZone];//以秒为单位返回当前应用程序与世界标准时间(格林尼治时间)的时差NSInteger interval = [zone secondsFromGMTForDate:date];//补充时差后为当前时间NSDate *localeDate = [date dateByAddingTimeInterval:interval];NSLog(@"%ld",(long)interval);NSLog(@"%@",localeDate);}@end
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NSCalendar 日历