美文网首页
时间戳跟标准时间相互转换

时间戳跟标准时间相互转换

作者: BetterComingDay | 来源:发表于2017-03-14 18:44 被阅读14次

    本段代码的主要功能是取出装满标准时间的数组里边最大的值。并以标准时间输出

        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;
    

    相关文章

      网友评论

          本文标题:时间戳跟标准时间相互转换

          本文链接:https://www.haomeiwen.com/subject/ccohnttx.html