时间戳跟标准时间相互转换
2017-03-14 本文已影响14人
BetterComingDay
本段代码的主要功能是取出装满标准时间的数组里边最大的值。并以标准时间输出
NSMutableArray *arrayMSecond = [NSMutableArray arrayWithCapacity:2];
for(NSString *date in arrayM){
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//设置时区,这个对于时间的处理有时很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* dateSecond = [formatter dateFromString:date]; //------------将字符串按formatter转成nsdate
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[dateSecond timeIntervalSince1970]] integerValue];
[arrayMSecond addObject:@(timeSp)];
}
NSInteger max = [[arrayMSecond objectAtIndex:0] integerValue];
for (int i=0; i<arrayMSecond.count-1; i++) {
if(max < [[arrayMSecond objectAtIndex:i+1] integerValue]){
max = [[arrayMSecond objectAtIndex:i+1] integerValue];
}
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//设置时区,这个对于时间的处理有时很重要
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:max];
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
self.presellEndDate = confromTimespStr;