欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

时间运算之Calendars

程序员文章站 2022-03-16 15:10:17
...

Creating a Calendar

 

 

1:You create a calendar object by specifying an identifier for the calendar you want. Mac OS X provides data for several different calendars—Buddhist, Chinese, Gregorian, Hebrew, Islamic, and Japanese—specified by constants in NSLocale.

 

 

NSCalendar *japaneseCalendar = [[NSCalendar alloc]
        initWithCalendarIdentifier:NSJapaneseCalendar];

 

 

2:You can get the calendar for the user's preferred locale (or in general from any NSLocale object) using the key NSLocaleCalendar,

 

NSCalendar *usersCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:
        [[NSLocale currentLocale] objectForKey:NSLocaleCalendar]];

 

 

3:or most easily using the NSCalendar method currentCalendar. 

 

 

NSCalendar *currentCalendar = [NSCalendar currentCalendar];

 

 


 
Calendrical Calculations

 

取得时间差

 

NSDate *startDate = ...;
NSDate *endDate = ...;
 
NSCalendar *gregorian = [[NSCalendar alloc]
        initWithCalendarIdentifier:NSGregorianCalendar];
 
unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
 
NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:startDate
                                          toDate:endDate options:0];
int months = [components month];
int days = [components day];

 

 

api:

 

 

1:从NSDate 中取得NSDateComponents 对象

 

Returns a NSDateComponents object containing a given date decomposed into specified components.

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date

 

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];

 

 

2:取得时间差

Returns, as an NSDateComponents object using specified components, the difference between two supplied dates.

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts

 

NSDate *startDate = ...;
NSDate *endDate = ...;
unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];
int months = [comps month];
int days = [comps day];

 

3:在给定的NSDate 上增加几天,几年等。。。。

 

Returns a new NSDate object representing the absolute time calculated by adding given components to a given date.

- (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSUInteger)opts

 

NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:2];
[comps setDay:3];
NSDate *date = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
[comps release];

 

4:取得一个指定的时间

Returns a new NSDate object representing the absolute time calculated from given components.

- (NSDate *)dateFromComponents:(NSDateComponents *)comps

 

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1965];
[comps setMonth:1];
[comps setDay:6];
[comps setHour:14];
[comps setMinute:10];
[comps setSecond:0];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];

 

 


 Calendar Units
Specify calendrical units such as day and month.

 

enum {
   NSEraCalendarUnit = kCFCalendarUnitEra,
   NSYearCalendarUnit = kCFCalendarUnitYear,
   NSMonthCalendarUnit = kCFCalendarUnitMonth,
   NSDayCalendarUnit = kCFCalendarUnitDay,
   NSHourCalendarUnit = kCFCalendarUnitHour,
   NSMinuteCalendarUnit = kCFCalendarUnitMinute,
   NSSecondCalendarUnit = kCFCalendarUnitSecond,
   NSWeekCalendarUnit = kCFCalendarUnitWeek,
   NSWeekdayCalendarUnit = kCFCalendarUnitWeekday,
   NSWeekdayOrdinalCalendarUnit = kCFCalendarUnitWeekdayOrdinal
};

相关标签: OS