最近做的一个项目遇到挺多NSDate的计算,以及与NSString的转换。在此写个笔记仅做记录。
//NSDate与NSString的相互转换(NSDateFormatter的格式位数一定要跟NSString的一样)
NSString *dateString = @"2016-12-15 12:35";
NSDateFormatter *formatDate = [NSDateFormatter new];
[formatDate setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date = [formatDate dateFromString:dateString];
//获取NSDate的年月日时分秒
NSString *startDateString = @"1969-12-31 23:00:00";
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:timesInfo.startTime];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
NSDateComponents *startDateConponent = [currentCalendar components:unitFlags fromDate:startDate];
NSInteger startHour = [startDateConponent hour];
NSInteger startMinute = [startDateConponent minute];
所用到的对象类型有NSDate,NSDateFormatter,NSCalendar,NSUInteger,NSDateComponents。
网友评论