c# - 从 DataContractJsonSerializer 格式化日期到 iOS

标签 c# iphone .net objective-c ios

我从 json 文件中收到以下格式的日期。我不确定,但我认为它是由 .Net 框架中的 DataContractJsonSerializer 类格式化的。日期看起来像这样

    \/Date(1255993200000+0100)\/

我想知道是否有人知道我如何将其转换为 iOS 上的正常日期,或者我是否需要做任何事情来改变它。

谢谢,

最佳答案

试试这个

-(NSDate*)mfDateFromDotNetJSONString:(NSString *)string
{
    static NSRegularExpression *dateRegEx = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil];
    });
    NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];

    if (regexResult)
    {
        // milliseconds
        NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0;
        // timezone offset
        if ([regexResult rangeAtIndex:2].location != NSNotFound) {
            NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]];
            // hours
            seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0;
            // minutes
            seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0;
        }

        return [NSDate dateWithTimeIntervalSince1970:seconds];
    }
    return nil;
}

并使用

传递字符串

[NSString stringWithFormat:...] 到这个函数

这对我有帮助,希望对你有帮助

关于c# - 从 DataContractJsonSerializer 格式化日期到 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295464/

相关文章:

c# - 如何在 SQL Server C# 中向图像列插入空值

ios - Cordova iPhone 6/6+ 闪屏图像

iphone - 放大CATiledLayer时如何在未转换的上下文上绘制?

c# - XmlWriter 设计背后的 OOP 推理是什么?

c# - 将所有者设置为来自另一个流程表单的表单

c# - 在 C# 中为我的应用程序编写 API

c# - 用于 lzo.net 的最新版本的 lzo.dll(内存压缩)

c# - 标签滚动效果

iphone - 是重用 View Controller 更好还是分离逻辑更好

c# - 类变量之间的歧义