根据时间或时间戳获得时分秒年月日

不多说话,直接上代码:
传入的时间格式是2016-12-12 12:22:22
如果需要传入时间戳直接将时间戳转换成NSDate就行了
+(NSDictionary )cutTimeForHourMinment:(NSString )time
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
NSDate *timeDate = [dateFormatter dateFromString:time];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [calendar components:unitFlags fromDate:timeDate];

return  @{
          @"year"            :          [NSString stringWithFormat:@"%ld",[comps year]],
          @"month"           :          [NSString stringWithFormat:@"%ld",[comps month]],

/* @”isleapMonth” : [NSString stringWithFormat:@”%ld”,[comps isLeapMonth]],//是否有闰月*/
@”week” : [NSString stringWithFormat:@”%ld”,[comps weekday]],
@”day” : [NSString stringWithFormat:@”%ld”,[comps day]],
@”hour” : [NSString stringWithFormat:@”%ld”,[comps hour]],
@”minute” : [NSString stringWithFormat:@”%ld”,[comps minute]],
@”second” : [NSString stringWithFormat:@”%ld”,[comps second]],
};
}

猜你喜欢

转载自blog.csdn.net/ZhaiAlan/article/details/52025406
今日推荐