【问题标题】:NSTimeInterval format for POST with JSON objective-c to DateTime .net format用于 POST 的 NSTimeInterval 格式,带有 JSON 目标-c 到 DateTime .net 格式
【发布时间】:2011-06-10 13:00:05
【问题描述】:

我检索 NSTimeInterval 如下:

NSTimeInterval milisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000);

记录此: UNIX 时间戳:“1307710190993.865967”

我只希望点之前的值使用 JSON 发送,如下所示:

"/Date(1307710131826+0200)/"

有什么建议吗?

提前致谢,

【问题讨论】:

    标签: objective-c nsdate nstimeinterval


    【解决方案1】:

    获取自 1970 年 UNIX 时间戳以来的当前时间戳(以毫秒为单位)。

    NSTimeInterval millisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000);
    

    将milliesecondedDate 格式化为不带小数的字符串。

    NSString* formattedMilliseconds = [NSString stringWithFormat:@"%.0f", millisecondedDate];
    

    以 DateTime .net 格式设置时间戳。

    NSString *startOfDateFormat = @"/Date(";
    NSString *endOfDateFormat = @"+0200)/";
    NSString *dateTimeNow = [NSString stringWithFormat:@"%@%@%@",startOfDateFormat, formattedMilliseconds, endOfDateFormat];
    

    这样做可能有更好的解决方案,但目前这对我有用。

    【讨论】:

    • endOfDateFormat 取决于夏令时。这应该是计算出来的,而不是硬编码的。
    • 是的,我应该指出这一点。但问题是关于时间戳的格式结构。为了示例的清晰,它只是被硬编码。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    相关资源
    最近更新 更多