日历 NSCalendar

試著忘記壹切 提交于 2020-04-17 04:24:01

【推荐阅读】微服务还能火多久?>>>


#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来封装

//需要注意的是,只有明确指定了unitFlagsNSDateComponents相应的那一部分才有值。

- (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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!