ca88编程iOS--开采中当时间越过夏令时

作者:ca88编程
///获取GMT格林尼治标准时间NSDate *date = [NSDate date];NSLog(@"date = %@",date);///获取当前北京时间:(注意北京时间=GMT时间 8h)NSDate *bjDate = [NSDate dateWithTimeIntervalSinceNow:8*60*60];NSLog(@"bjDate = %@", bjDate);输出为:2016-06-28 18:54:10.921 Tank[3159:256082] date = 2016-06-28 10:54:10  00002016-06-28 18:54:10.922 Tank[3159:256082] bjDate = 2016-06-28 18:54:10  0000

[comps setYear:2004];

    使用Category来测算分裂时代(AD|BC)八个日子的天数: 
C代码  
@implementation NSCalendar (MyOtherMethod)    
    
-(NSInteger) daysFromDate:(NSDate *) startDate toDate:(NSDate *) endDate {    
    
     NSCalendarUnit units=NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;    
    
     NSDateComponents *comp1=[self components:units fromDate:startDate];    
     NSDateComponents *comp2=[self components:units fromDate endDate];    
    
     [comp1 setHour:12];    
     [comp2 setHour:12];    
    
     NSDate *date1=[self dateFromComponents: comp1];    
     NSDate *date2=[self dateFromComponents: comp2];    
    
     return [[self components:NSDayCalendarUnit fromDate:date1 toDate:date2 options:0] day];    
}    
    
@end   

    获得当前礼拜中的周末(使用格里高利历): 
C代码  
NSDate *today = [[NSDate alloc] init];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
// Get the weekday component of the current date    
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];    
    
/*   
Create a date components to represent the number of days to subtract from the current date.   
   
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question.  (If today is Sunday, subtract 0 days.)   
*/    
    
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];    
    
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];    
    
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];    
    
/*   
Optional step:   
beginningOfWeek now has the same hour, minute, and second as the original date (today).   
   
To normalize to midnight, extract the year, month, and day components and create a new date from those components.   
*/    
    
NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: beginningOfWeek];    
    
beginningOfWeek = [gregorian dateFromComponents:components];    

看来这一段,即刻大悟了吗,假若你的工程刚刚用到总结时间,注意一下那点呢,苹果也正是diao,这几个历史事件都被它算进系统去了。

NSDateComponents *components = [[NSDateComponents alloc] init];

NSDate存款和储蓄的是世界标准时(UTC),输出时索要依赖时区调换为本土时间 

NSDate存款和储蓄的是社会风气标准时(UTC),输出时必要依据时区转换为本地时间 

终于,你想起来了,有个叫做“夏令时”的东西,赶紧百度下啊:

[components setDay:6];

Dates 
        NSDate类提供了创立date,比较date以及总括三个date之间间隔的作用。Date对象是不行改造的。 
        倘使你要创立date对象并代表目前些天子,你能够alloc二个NSDate对象并调用init早先化: 
C代码  
NSDate *now = [[NSDate alloc] init];    
  
          只怕使用NSDate的date类方法来创立三个日子对象。假设您必要与当今天期不相同的日期,你能够选择NSDate的initWith提姆eInterval...或dateWithTimeInterval...方法,你也能够使用更复杂的calendar或date components对象。 

下面包车型客车示范展现了怎样从八个日历置换成另一个日历: 
C代码  
NSDateComponents *comps = [[NSDateComponents alloc] init];    
    
[comps setDay:6];    
[comps setMonth:5];    
[comps setYear:2004];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDate *date = [gregorian dateFromComponents:comps];    
    
[comps release];    
[gregorian release];    
    
NSCalendar *hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];    
    
NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;    
    
NSDateComponents *components = [hebrew components:unitFlags fromDate:date];    
    
NSInteger day = [components day]; // 15    
NSInteger month = [components month]; // 9    
NSInteger year = [components year]; // 5764    

那二日巧合地发掘二个小意思:偶尔在iOS开采中我们会用到NSDate、NSCalendar、NSDateComponents来创制二个岁月,例如上面包车型地铁:

NSInteger day = [weekdayComponents day];

历法总括 

    怎么样能够计算出二十三日的第一天(依据系统的日历设置): 
C代码  
NSDate *today = [[NSDate alloc] init];    
    
NSDate *beginningOfWeek = nil;    
    
BOOL ok = [gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:NULL forDate: today];    

ca88编程 1神州夏令时

//    min = [comps minute];

C代码  
-(NSString *)getDate  
{  
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];  
[formatter setDateFormat:@"yyyy-MM-dd EEEE HH:mm:ss a"];  
NSString *locationString=[formatter stringFromDate: [NSDate date]];  
[formatter release];  
return locationString;  
}  

        为了确定保证科学的行为,您必需有限支撑使用的零件在日历上是有含义的。钦定“出界”日历组件,如二个-6或八月三17日在农历中的日期值发生未定义的表现。 

有心人的您或许那会相应在数手指头了吗,1,2,3....7,8,9 !!!什么?那什么鬼?再来一遍:1,2,3......7,8,9 !!!怎么回事,不是说新加坡时间与维生霉素T时间相差8钟头吧?为什么会9时辰,然后你换回当前时光,输入二〇一五,咦?平常了,再回来一九九〇,又出标题了,壹玖玖贰依旧有标题,换来1994又健康了,换来1989年5月,又健康了,那是何许鬼????不会只坑作者那一个一九八八年诞生的呢?

NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];

        若是要对NSDate对象开展相比,能够动用isEqualToDate:, compare:, laterDate:和 earlierDate:方法。这几个方式都进展标准相比,也正是说这个方法会一向准确比较到NSDate对象中秒一级。举例,你可能相比七个日子,即便他们中间的间距在一分钟之内则感到那多个日子是十二分的。在这种情景下使用,timeIntervalSinceDate:方法来对多个日子实行比较。上边包车型大巴代码实行了示范: 
C代码  
if (fabs([date2 timeIntervalSinceDate:date1]) < 60) ...   

        你也足以创立贰个不带年份的NSDate对象,这样的操作系统会自动生成一个年度,但在末端的代码中不会选用其自动生成的年度。 
C代码  
NSDateComponents *components = [[NSDateComponents alloc] init];    
    
[components setMonth:11];    
[components setDay:7];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDate *birthday = [gregorian dateFromComponents:components];   

NSCalendar *currentCalendar = [NSCalendar currentCalendar];NSDateComponents *components = [[NSDateComponents alloc]init];components.year = 1990;components.month = 05;components.day = 20;components.hour = 13;components.minute =22;components.second = 16;NSDate *aDate = [currentCalendar dateFromComponents:components];NSLog(@"aDate = %@",aDate);输出为:注意这里输出为04:22:162016-06-28 18:54:10.922 Tank[3159:256082] aDate = 1990-05-20 04:22:16  0000

NSInteger month = [components month]; // 9

1、获取当前几日子 

来源: 

其它,Foundation框架为此为大家提供了NSCalendar类,NSCalendar由更自然的日子组成,例如日,月,星期等,能够使用NSDateComponents 和NSCalendar 来创建贰个NSDate对象。如:

NSDateComponents *components = [[NSDateComponents alloc] init];

来源: 

    决断二个日子是或不是在时下七日内(使用格里高利历): 
C代码  
-(BOOL)isDateThisWeek:(NSDate *)date {    
    
     NSDate *start;    
     NSTimeInterval extends;    
    
     NSCalendar *cal=[NSCalendar autoupdatingCurrentCalendar];    
     NSDate *today=[NSDate date];    
    
     BOOL success= [cal rangeOfUnit:NSWeekCalendarUnit startDate:&start interval: &extends forDate:today];   
    
     if(!success)    
        return NO;    
    
     NSTimeInterval dateInSecs = [date timeIntervalSinceReferenceDate];    
     NSTimeInterval dayStartInSecs= [start timeIntervalSinceReferenceDate];    
    
     if(dateInSecs > dayStartInSecs && dateInSecs < (dayStartInSecs extends)){    
          return YES;    
     }    
     else {    
          return NO;    
     }    
}    

//    day = [comps day];

        NSCalendar定义了不相同的日历,满含东正教历,格里高利历等(这么些都与系统提供的本地化设置相关)。NSCalendar与NSDateComponents对象紧凑相关。 

        若是要对NSDate对象开展比较,能够运用isEqualToDate:, compare:, laterDate:和 earlierDate:方法。这一个主意都开展精确比较,也正是说那些方法会一贯正确对比到NSDate对象中秒一流。举例,你恐怕相比多少个日子,尽管她们中间的距离在一分钟以内则感到那多少个日子是特出的。在这种意况下行使,timeIntervalSinceDate:方法来对四个日子举行相比。上边的代码进行了演示: 
C代码  
if (fabs([date2 timeIntervalSinceDate:date1]) < 60) ...   

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

C代码  
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];  
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];  
NSString *locationString=[formatter stringFromDate: [NSDate date]];  

Dates 
        NSDate类提供了创立date,比较date以及总结多个date之间距离的成效。Date对象是不行改换的。 
        借使您要创设date对象并代表目前几日期,你能够alloc一个NSDate对象并调用init初步化: 
C代码  
NSDate *now = [[NSDate alloc] init];    
  
          恐怕采用NSDate的date类方法来创设一个日期对象。假若您须求与当下日子分歧的日子,你可以使用NSDate的initWithTimeInterval...或dateWithTimeInterval...方法,你也能够应用更目迷五色的calendar或date components对象。 

NSDateComponents *components = [hebrew components:unitFlags fromDate:date];

        usersCalendar和currentCalendar对象是相等的,尽管他们是例外的靶子。 

    获得七个日子之间的间距: 
C代码  
NSDate *startDate = ...;    
NSDate *endDate = ...;    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;    
    
NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];    
    
NSInteger months = [components month];    
NSInteger days = [components day];  

// 使用加多时间距离的不二秘诀来生成NSDate对象:

        要将一个日子对象深入分析到对应的date components,你能够利用NSCalendar的components:fromDate:方法。其它日期本人,你须求钦赐NSDateComponents对象回来组件。 
C代码  
NSDate *today = [NSDate date];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];    
    
NSInteger day = [weekdayComponents day];    
NSInteger weekday = [weekdayComponents weekday];  

1、获取当前时刻 

//        日历对象封装了对系统日期的盘算,饱含那年开端,总天数以及划分。你将选用日历对象对相对日期与date components(包括年,月,日,时,分,秒)举行改变。

下边包车型客车亲自去做呈现了怎么从贰个日历置换来另三个日历: 
C代码  
NSDateComponents *comps = [[NSDateComponents alloc] init];    
    
[comps setDay:6];    
[comps setMonth:5];    
[comps setYear:2004];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDate *date = [gregorian dateFromComponents:comps];    
    
[comps release];    
[gregorian release];    
    
NSCalendar *hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];    
    
NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;    
    
NSDateComponents *components = [hebrew components:unitFlags fromDate:date];    
    
NSInteger day = [components day]; // 15    
NSInteger month = [components month]; // 9    
NSInteger year = [components year]; // 5764    

        日历对象封装了对系统日期的总结,饱含这年起先,总天数以及划分。你将应用日历对象对相对日期与date components(包括年,月,日,时,分,秒)进行更动。 

// 轻易记录一下ios里面获得系统时间的法子,现在有别的更详细的功用更创新。

    判定八个日期是还是不是在时下七天内(使用格里高利历): 
C代码  
-(BOOL)isDateThisWeek:(NSDate *)date {    
    
     NSDate *start;    
     NSTimeInterval extends;    
    
     NSCalendar *cal=[NSCalendar autoupdatingCurrentCalendar];    
     NSDate *today=[NSDate date];    
    
     BOOL success= [cal rangeOfUnit:NSWeekCalendarUnit startDate:&start interval: &extends forDate:today];   
    
     if(!success)    
        return NO;    
    
     NSTimeInterval dateInSecs = [date timeIntervalSinceReferenceDate];    
     NSTimeInterval dayStartInSecs= [start timeIntervalSinceReferenceDate];    
    
     if(dateInSecs > dayStartInSecs && dateInSecs < (dayStartInSecs extends)){    
          return YES;    
     }    
     else {    
          return NO;    
     }    
}    

        要将三个日子对象深入分析到相应的date components,你能够接纳NSCalendar的components:fromDate:方法。另外日期本身,你须要钦赐NSDateComponents对象回来组件。 
C代码  
NSDate *today = [NSDate date];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];    
    
NSInteger day = [weekdayComponents day];    
NSInteger weekday = [weekdayComponents weekday];  

//    摘自 diqun1314的专栏

日子格式如下: 
y  年  Year  1996; 96  
M  年中的月份  Month  July; Jul; 07  
w  年中的周数  Number  27  
W  月份中的周数  Number  2  

    在现阶段日子增进三个三刻钟: 
C代码  
NSDate *today = [[NSDate alloc] init];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];    
    
[offsetComponents setHour:1];    
[offsetComponents setMinute:30];    
    
// Calculate when, according to Tom Lehrer, World War III will end    
NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];    

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    在当前时光累加多少个半钟头: 
C代码  
NSDate *today = [[NSDate alloc] init];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];    
    
[offsetComponents setHour:1];    
[offsetComponents setMinute:30];    
    
// Calculate when, according to Tom Lehrer, World War III will end    
NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];    

        成立一定期间距离的NSDate对象: 
C代码  
NSTimeInterval secondsPerDay = 24 * 60 * 60;    
    
NSDate *tomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay];    
    
NSDate *yesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];    
    
[tomorrow release];    
[yesterday release];    
  
        使用增添时间间隔的法子来生成NSDate对象: 
C代码  
NSTimeInterval secondsPerDay = 24 * 60 * 60;    
    
NSDate *today = [[NSDate alloc] init];    
NSDate *tomorrow, *yesterday;    
    
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];    
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];    
    
[today release];    

//    同样你也足以从NSDateComponents对象来创立NSDate对象:

    使用Category来计算同不经常期(AD|BC)八个日子早上时期的时局: 
C代码  
@implementation NSCalendar (MySpecialCalculations)    
    
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate {    
     NSInteger startDay=[self ordinalityOfUnit:NSDayCalendarUnit inUnit: NSEraCalendarUnit forDate:startDate];    
    
     NSInteger endDay=[self ordinalityOfUnit:NSDayCalendarUnit inUnit: NSEraCalendarUnit forDate:endDate];    
    
     return endDay-startDay;    
}    
    
@end    

NSCalendar & NSDateComponents 

if (fabs([date2 timeIntervalSinceDate:date1]) < 60) ...

    得到八个日子之间的距离: 
C代码  
NSDate *startDate = ...;    
NSDate *endDate = ...;    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;    
    
NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];    
    
NSInteger months = [components month];    
NSInteger days = [components day];  

    使用Category来计量不一样一时间代(AD|BC)三个日子的大运: 
C代码  
@implementation NSCalendar (MyOtherMethod)    
    
-(NSInteger) daysFromDate:(NSDate *) startDate toDate:(NSDate *) endDate {    
    
     NSCalendarUnit units=NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;    
    
     NSDateComponents *comp1=[self components:units fromDate:startDate];    
     NSDateComponents *comp2=[self components:units fromDate endDate];    
    
     [comp1 setHour:12];    
     [comp2 setHour:12];    
    
     NSDate *date1=[self dateFromComponents: comp1];    
     NSDate *date2=[self dateFromComponents: comp2];    
    
     return [[self components:NSDayCalendarUnit fromDate:date1 toDate:date2 options:0] day];    
}    
    
@end   

[components setWeekday:2]; // Monday

        为了确认保证科学的行为,您必需保险使用的零件在日历上是有含义的。钦命“出界”日历组件,如二个-6或11月二11日在农历中的日期值爆发未定义的表现。 

历法总括 

NSDate *date = [gregorian dateFromComponents:components];

    怎么着可以总结出二三十一日的首后天(依据系统的日历设置): 
C代码  
NSDate *today = [[NSDate alloc] init];    
    
NSDate *beginningOfWeek = nil;    
    
BOOL ok = [gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:NULL forDate: today];    

除此以外的方式: 

[comps setDay:6];

NSCalendar & NSDateComponents 

        你能够因而NSCalendar对象的currentCalendar方法来博取当前系统客户安装的日历。 
C代码  
NSCalendar *currentCalendar = [NSCalendar currentCalendar];    
NSCalendar *japaneseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSJapaneseCalendar];    
NSCalendar *usersCalendar = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar];    

NSDate *now;

        成立一定期间间隔的NSDate对象: 
C代码  
NSTimeInterval secondsPerDay = 24 * 60 * 60;    
    
NSDate *tomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay];    
    
NSDate *yesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];    
    
[tomorrow release];    
[yesterday release];    
  
        使用增加时间距离的不二等秘书诀来生成NSDate对象: 
C代码  
NSTimeInterval secondsPerDay = 24 * 60 * 60;    
    
NSDate *today = [[NSDate alloc] init];    
NSDate *tomorrow, *yesterday;    
    
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];    
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];    
    
[today release];    

        usersCalendar和currentCalendar对象是特别的,纵然他们是例外的靶子。 

[components setYear:2004];

        你能够采取NSDateComponents对象来表示三个日期对象的机件——比方年,月,日和小时。假设要使八个NSDateComponents对象有含义,你必得将其与三个日历对象相关联。上边包车型地铁代码示例了怎么着成立一个NSDateComponents对象: 
C代码  
NSDateComponents *components = [[NSDateComponents alloc] init];    
    
[components setDay:6];    
[components setMonth:5];    
[components setYear:2004];    
    
NSInteger weekday = [components weekday]; // Undefined (== NSUndefinedDateComponent)    

一律你也能够从NSDateComponents对象来创设NSDate对象:  
C代码  
NSDateComponents *components = [[NSDateComponents alloc] init];    
    
[components setWeekday:2]; // Monday    
[components setWeekdayOrdinal:1]; // The first Monday in the month    
[components setMonth:5]; // May    
[components setYear:2008];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
NSDate *date = [gregorian dateFromComponents:components];    

NSDate *date = [gregorian dateFromComponents:comps];

    得到当前礼拜中的星期六(使用格里高利历): 
C代码  
NSDate *today = [[NSDate alloc] init];    
    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    
// Get the weekday component of the current date    
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];    
    
/*   
Create a date components to represent the number of days to subtract from the current date.   
   
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question.  (If today is Sunday, subtract 0 days.)   
*/    
    
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];    
    
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];    
    
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];    
    
/*   
Optional step:   
beginningOfWeek now has the same hour, minute, and second as the original date (today).   
   
To normalize to midnight, extract the year, month, and day components and create a new date from those components.   
*/    
    
NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: beginningOfWeek];    
    
beginningOfWeek = [gregorian dateFromComponents:components];    

    使用Category来计量同不时期(AD|BC)四个日子晌午之间的天命: 
C代码  
@implementation NSCalendar (MySpecialCalculations)    
    
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate {    
     NSInteger startDay=[self ordinalityOfUnit:NSDayCalendarUnit inUnit: NSEraCalendarUnit forDate:startDate];    
    
     NSInteger endDay=[self ordinalityOfUnit:NSDayCalendarUnit inUnit: NSEraCalendarUnit forDate:endDate];    
    
     return endDay-startDay;    
}    
    
@end    

//    Dates

        你能够因而NSCalendar对象的currentCalendar方法来收获当前系统客户安装的日历。 
C代码  
NSCalendar *currentCalendar = [NSCalendar currentCalendar];    
NSCalendar *japaneseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSJapaneseCalendar];    
NSCalendar *usersCalendar = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar];    

C代码  
-(NSString *)getDate  
{  
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];  
[formatter setDateFormat:@"yyyy-MM-dd EEEE HH:mm:ss a"];  
NSString *locationString=[formatter stringFromDate: [NSDate date]];  
[formatter release];  

//    4--星期三

        日历对象封装了对系统日期的一个钱打二16个结,包涵那一年开头,总天数以及划分。你将使用日历对象对相对日期与date components(蕴含年,月,日,时,分,秒)举办改换。 

C代码  
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];  
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];  
NSString *locationString=[formatter stringFromDate: [NSDate date]];  

本文由ca88发布,转载请注明来源

关键词: ca88网址 yzc567亚洲城 iOS 夏令时 时间