【问题标题】:Extract time from ISO8601 string [closed]从 ISO8601 字符串中提取时间 [关闭]
【发布时间】:2013-06-03 05:10:41
【问题描述】:

我有一个 ISO8601 字符串

2013-05-11 05:54:07.589698

无论如何,是否可以在不进行字符串操作的情况下从中提取时间?

【问题讨论】:

  • 谷歌NSDateFormatter
  • 此外,这不是 ISO8601 字符串
  • 感谢 h2c03!有时我只需要知道在哪里看。原谅无知并感谢您的帮助。抱歉,这不是 ISO8601 格式的时间戳字符串?
  • 好吧,从我读到的内容来看,如果不操作字符串前两个将两者分开,就无法将这个 2013-05-11 05:54:07.589698 变成这个 05:54:07 。这是正确的吗?

标签: ios objective-c nsdate iso8601


【解决方案1】:

类似问题有很多答案,如果你还没有找到就发布这个答案

NSString *inputString = @"2013-05-11 05:54:07.589698";

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];

NSDate *date = [dateFormatter dateFromString:inputString];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *timeString = [dateFormatter stringFromDate:date];

NSLog(@"%@",timeString);

【讨论】:

  • 上帝我爱你……非常非常非常。谢谢!
猜你喜欢
  • 2023-03-18
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 2018-05-13
  • 2015-10-04
  • 2012-11-05
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多