【问题标题】:Convert Timestamp to NSDate not working将时间戳转换为 NSDate 不起作用
【发布时间】:2013-07-24 10:56:39
【问题描述】:

我在stackoverflow上遇到了很多链接,并尝试实现代码以将这种格式的时间戳2013-07-20T12:23:54.411+05:30转换为这种格式的NSDate

2013 年 7 月 20 日(星期二)12:23:54 或 1 分钟前、2 天前格式。

我已经实现了以下代码。 toDate 为 nil,即 date1 给出 nil 值。似乎 Xcode 对这个问题很讽刺

我的意思是,你认为该操作应该是什么意思 toDate 为零?目前已经避免了一个例外。一些 这些错误将与此投诉一起报告,然后 进一步的违规行为只会默默地做任何随机的事情 结果来自 nil。

谁能告诉我哪里出错了?

     NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
    [dateForm setDateFormat:@"yyyy-mm-ddTHH:mm:ssssZ"];
    [dateForm setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    [dateForm setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [cal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSDate *date1 = [dateForm dateFromString:string];
    NSDate *date2 = [NSDate date];
    unsigned int unitFlags = NSDayCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;

    // gets the difference between the current date and also the date and time in the timestamp of the data
    NSDateComponents *diffComps = [cal components:unitFlags fromDate:date2 toDate:date1 options:0];

    int year = ABS([diffComps year]);
    int month = ABS([diffComps month]);
    int day = ABS([diffComps day]);
    int hour = ABS([diffComps hour]);
    int minute = ABS([diffComps minute]);
    int seconds = ABS([diffComps second]);
    NSString *displayTime;

    if (year == 0) {

        if (month == 0) {

            if (day == 0) {

                if (hour == 0){

                    if (minute == 0) {

                        displayTime = [NSString stringWithFormat:@"about %d secs ago",seconds];
                    }
                    else {

                        displayTime = [NSString stringWithFormat:@"about %d mins ago",minute];
                    }
                }
                else {

                    displayTime = [NSString stringWithFormat:@"about %d hours ago",hour];
                }
            }
            else {
                displayTime = [NSString stringWithFormat:@"about %d days ago",day];
            }
        }
        else {

            displayTime = [NSString stringWithFormat:@"about %d months ago",month];
        }
    }
    else {

        displayTime = [NSString stringWithFormat:@"about %d years ago",year];
    }

编辑: 这适用于我在 appdelegate 中创建的字符串。但不是我在单例类中创建的字符串。

【问题讨论】:

  • “出了什么问题”?
  • 您的日期格式错误。找到 NSDateFormatter 的文档,然后按照大约 6 个链接长的链获取日期格式代码。答案就在那里。我会指给你看,但你不知道如何自己找到它。 (或者你可以在过去被问到这个问题时搜索几十次之一。)
  • 我在 SO 和 google 上搜索了很多链接。大多数解决方案都是针对 UNIX 时间戳的。我知道我的日期格式是错误的,但我没有得到正确的解决方案。
  • 您可能需要四个 ZZZZ 字符作为日期格式的时区。
  • "yyyy-MM-dd'T'HH:mm:SSssssZZZZ" 怎么样?

标签: nsdate nsdateformatter


【解决方案1】:

您没有使用正确的时间格式;我建议这样做:

yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ

【讨论】:

  • 很抱歉弄乱了您的格式。但根据NSLog(@"%@", [df stringFromDate:[df dateFromString:@"2013-07-20T12:23:54.411+05:30"]]);,现在应该是正确的。
  • @MatthiasBauch 没问题。干杯。
  • 再澄清一点,此代码适用于一个字符串,而不适用于具有相同时间格式的另一个字符串...知道为什么吗?
  • @SharanyaKM 您是否使用了 Matthias Bauch 提供的更新格式?
猜你喜欢
  • 2012-03-19
  • 2011-09-18
  • 1970-01-01
  • 2016-02-26
  • 2022-12-03
  • 2012-06-10
  • 2012-06-10
相关资源
最近更新 更多