ios 时间的比较&&获得系统本地时间指定时区
2013-09-27 12:08
453 查看
ios 时间的比较&&获得系统本地时间指定时区
- (BOOL)isEqualToDate:(NSDate *)otherDate;
与otherDate比较,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
与anotherDate比较,返回较早的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
与anotherDate比较,返回较晚的那个日期
- (NSComparisonResult)compare:(NSDate *)other;
该方法用于排序时调用: .
当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending .
当实例保存的日期值早于anotherDate时返回NSOrderedAscending
// 获得本地时间指定时区 NSDate *dates = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
[formatter setTimeZone:timeZone];
NSString *loctime = [formatter stringFromDate:dates];
-(NSDate *) stringToDate:string
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:string];
[dateFormatter release]; return date;
}
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
int days = [comps day]; return days;
}
- (BOOL)isEqualToDate:(NSDate *)otherDate;
与otherDate比较,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
与anotherDate比较,返回较早的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
与anotherDate比较,返回较晚的那个日期
- (NSComparisonResult)compare:(NSDate *)other;
该方法用于排序时调用: .
当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending .
当实例保存的日期值早于anotherDate时返回NSOrderedAscending
// 获得本地时间指定时区 NSDate *dates = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
[formatter setTimeZone:timeZone];
NSString *loctime = [formatter stringFromDate:dates];
-(NSDate *) stringToDate:string
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:string];
[dateFormatter release]; return date;
}
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
int days = [comps day]; return days;
}
相关文章推荐
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- 学习Objective-C:入门手册
- ios开发对配置文件plist的操作
- iphone5和4的屏幕兼容问题
- 后天工作室简介
- iOS开发笔记
- 广州知名游戏公司招贤纳士啦~~~
- 谈谈怎么最快学好iOS开发(个人看法)
- ios长按事件
- iOS 获取应用版本号
- iOS中定时器NSTimer的使用
- 《Objective-C基础教程》学习笔记第二章
- 《Objective-C基础教程》学习笔记第三-六章
- 《Objective-C基础教程》学习笔记第八章
- 《Objective-C基础教程》学习笔记第九-十章
- 《Objective-C基础教程》学习笔记第十一-十三章
- IOS开发中的UIScreen、UIView、UIWindow和UIViewController