【问题标题】:How to convert it into time seconds, mins,hours,days,months years如何将其转换为时间秒、分钟、小时、天、月、年
【发布时间】:2016-10-08 07:32:26
【问题描述】:

团队,

我从服务器得到响应日期。

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

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

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

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

【问题讨论】:

  • 你能展示你尝试过的代码吗

标签: ios objective-c nsdate


【解决方案1】:
 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];

【讨论】:

  • 不是确切的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
相关资源
最近更新 更多