【问题标题】:Sudzc not converting 2012-03-07T21:08:23.6875-05:00 into an NSDateSudzc 未将 2012-03-07T21:08:23.6875-05:00 转换为 NSDate
【发布时间】:2012-03-24 14:17:33
【问题描述】:

我需要使用什么格式的字符串将“2012-03-07T21:08:23.6875-05:00”的字符串值解析为 NSDate?

背景:

我正在针对 .NET Web 服务使用 Sudzc ARC。 Web 方法返回两个 .NET DateTime 属性。虽然两者都是 .NET DateTime 属性,但 Web 方法以不同的日期/时间格式返回它们。

第一个 DateTime 的值在 SQL Server 中设置,并以这种格式“2012-03-07T21:08:23”返回一个值。 Sudzc 解析了这个伟大的...还没有问题。

第二个 DateTime 的值由 .NET 设置为 DateTime.Now.AddHours(24)。 Web 方法将其作为“2012-03-07T21:08:23.6875-05:00”的值返回,其中包含额外的毫秒和时区偏移量(例如:“.6875-05:00”)。 Sudzc 不会将此值解析为 NSDate,soap.m 方法 dateFromString 返回值为零。

我很确定这与 NSDateFormater 有关。 soap.m 附带以下内容...

+ (NSDateFormatter*)dateFormatter {
static NSDateFormatter* formatter;
if(formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    NSLocale* enUS = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale: enUS];
    [formatter setLenient: YES];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
}
return formatter;
}

// Converts a string to a date.
+ (NSDate*) dateFromString: (NSString*) value {
if([value rangeOfString:@"T"].length != 1) {
    value = [NSString stringWithFormat:@"%@T00:00:00.000", value];
}
if([value rangeOfString:@"."].length != 1) {
    value = [NSString stringWithFormat:@"%@.000", value];
}
if(value == nil || [value isEqualToString:@""]) { return nil; }

NSDate* outputDate = [[Soap dateFormatter] dateFromString: value];
return outputDate;
}

我尝试将格式字符串从“yyyy-MM-dd'T'HH:mm:ss.SSS”修改为“yyyy-MM-dd'T'HH:mm:ss.SSSSSS-zz: zz" 和其他变体,但 NSDate 总是返回 nil。

【问题讨论】:

  • NSDateFormatter 无法处理时区片段中的“:”。在使用 NSDateFormatter 解析之前,您必须删除该“:”。
  • 需要注意的是,iOS6 现在支持时区中的“:”。完整格式为“yyyy-MM-dd'T'HH:mm:ss.SSSSZZZZZ”。

标签: objective-c xcode nsdate nsdateformatter sudzc


【解决方案1】:

我使用它从 .NET Web 服务与 sudzc 进行转换

https://github.com/boredzo/iso-8601-date-formatter

ISO8601DateFormatter *isoDateFormatter = [[ISO8601DateFormatter alloc] init];
[isoDateFormatter dateFromString: ... ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2023-03-29
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多