美文网首页
iOS返回时间戳和当前时间对比,得出时分

iOS返回时间戳和当前时间对比,得出时分

作者: 玉思盈蝶 | 来源:发表于2020-06-11 22:31 被阅读0次

代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *nowDate = [NSDate date];
    NSInteger nowTime = [nowDate timeIntervalSince1970];
    NSInteger waitTiem = nowTime - (1591871601675 / 1000);
    NSLog(@"%@", [self getMMSSFromSS:waitTiem]);
    
    // 下一分时间
    NSInteger nextTime = (nowTime + 60) - (1591871601675 / 1000);
    NSLog(@"%@", [self getMMSSFromSS:nextTime]);
}

#pragma mark 传入 秒  得到 xx:xx:xx
-(NSString *)getMMSSFromSS:(NSInteger )seconds{
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    NSString *format_time = nil;
        format_time = [NSString stringWithFormat:@"%@:%@",str_hour,str_minute];
    return format_time;
}

打印如下:

03:56 分钟+1即是03:57


image.png

相关文章

网友评论

      本文标题:iOS返回时间戳和当前时间对比,得出时分

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