NSDate-常用操作及控制

[align=center]对于NSDate常用操作整理,如有不足请补充[/align]

1,获取当前时区时间

//初始化时间段
NSDate *date = [NSDate date];
//获取当前时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
//以秒为单位返回当前应用程序与世界标准时间(格林威尼时间)的时差
NSInteger interval = [zone secondsFromGMTForDate: date];
//补充时差后为当前时间
NSDate *localeDate = [date dateByAddingTimeInterval: interval];


2,将字符串格式化NSDate

NSString *dateStr=@"2013-08-13 20:28:40";
//格式化
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *fromdate=[dateFormatter dateFromString:dateStr];
//时区
NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
//同例1
NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
NSDate *fromDate = [fromdate dateByAddingTimeInterval: frominterval];
NSLog(@"fromdate=%@",fromDate);

//将NSDate转为NSString
NSString *strTime = [fromDate description];
//如果直接将NSDate格式化
NSDate *recv = [NSDate dateFromString:recvTime withFormat:@"yyyy-MM-dd HH:mm:ss Z"];


3,比较两个时间相差(年月日,时分秒)

double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];

long lTime = (long)intervalTime;
NSInteger iSeconds = lTime % 60;
NSInteger iMinutes = (lTime / 60) % 60;
NSInteger iHours = (lTime / 3600)$;
NSInteger iDays = lTime/60/60/24;
NSInteger iMonth = lTime/60/60/24/12;
NSInteger iYears = lTime/60/60/24/384;

NSLog(@"相差M年d月 或者 d日d时d分d秒", iYears,iMonth,iDays,iHours,iMinutes,iSeconds);


4,按日期排序

//与otherDate比较,相同返回YES
- (BOOL)isEqualToDate:(NSDate *)otherDate;

//与anotherDate比较,返回较早的那个日期
- (NSDate *)earlierDate:(NSDate *)anotherDate;

//与anotherDate比较,返回较晚的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;

//该方法用于排序时调用
- (NSComparisonResult)compare:(NSDate *)other;

//. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
//. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
//. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending


5,从NSDate中获取年月日,时分秒

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

NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |

NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *comps = [calendar components:unitFlags fromDate:date];

int year = [comps year];
int month = [comps month];
int day = [comps day];
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];


//获取数据有异常
// NSError *error = [[NSError alloc] init];
// NSData *pData = = [NSData dataWithContentsOfURL:URL options:NSDataReadingMapped error:&error];
// NSLog(@"download string end error = %d", [error code]);
// [error release];


判断12小时或24小时(制)

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"a"];
NSString *AMPMtext = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
if ([AMPMtext isEqualToString:@""]){
NSLog(@"24小时制");
}
else {
NSLog(@"12小时制");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值