如何将其转换为时间秒,分钟,小时,天,月

时间:2016-10-08 07:32:26

标签: ios objective-c nsdate

我收到了服务器的回复日期。

"createTime": "2016-10-07T19:22:34.3192343+00:00" 

根据日期更改,我想在设备上检查它(分钟,小时,天,月)。

是一种方法,如何计算完成时间? 基于服务请求"createTime" : "2016-10-07T19:22:34.3192343+00:00"

结果值应为30分钟。 2天后1:30分钟后1小时,这将是2天

1 个答案:

答案 0 :(得分:0)

 NSString* format = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    // Set up an NSDateFormatter for UTC time zone
    NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
    [formatterUtc setDateFormat:format];
    [formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    // Cast the input string to NSDate
    NSDate* utcDate = [formatterUtc dateFromString:[formatterUtc stringFromDate:@""2016-10-07T19:22:34.3192343+00:00" "]];

    // Set up an NSDateFormatter for the device's local time zone
    NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
    [formatterLocal setDateFormat:format];
    [formatterLocal setTimeZone:[NSTimeZone localTimeZone]];

    // Create local NSDate with time zone difference
    NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];


    NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:localDate];