这些日期是什么类型? [英] What types of dates are these?

查看:88
本文介绍了这些日期是什么类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样格式日期的plist:

I have a plist with this format date:

Mar 11, 2013 10:16:31 AM

在我的控制台中显示为

2013-03-11 16:16:31 +0000

而webservice正在返回在控制台中看起来像这样的东西:

whereas a webservice is returning something that in the console looks like this:

2013-03-01T18:21:45.231Z

如何将我的plist日期修改为与Web服务相同的格式?

How do I fix my plist date to the same format as the web service?

推荐答案

关于您的三种日期格式:

Regarding your three date formats:


  1. 当您在Xcode中的一个plist中查看 NSDate 时,该日期格式是当前语言环境中可读取的日期(但如果您在文本编辑器中查看plist,则看到它实际上写在 @yyyy' - 'MM' - 'dd'T'HH':'mm':'ss'Z'格式)。

  1. The first is just the date format when you look at a NSDate in a plist in Xcode, a human readable date in the current locale (but if you look at the plist in a text editor, you'll see it's actually written in @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'" format).

第二个是描述的默认格式方法a NSDate (例如您 NSLog a NSDate )。

The second is the default formatting from the description method a NSDate (e.g. you NSLog a NSDate).

第三个是 RFC 3339 / ISO 8601 格式(分秒),经常用于Web服务。

The third is RFC 3339/ISO 8601 format (with fractions of a second), often used in web services.

请参阅Apple的技术问答QA1480

除此之外,该技术说明不包括毫秒,所以你可能想要使用像 @yyyy' - 'MM' - 'dd'T'HH':'mm':'ss'。'SSS'Z',例如:

As an aside, that Technical Note doesn't include the milliseconds, so you might want to use something like @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'", for example:

NSDate *date = [NSDate date];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = enUSPOSIXLocale;
formatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'";
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSString *dateString = [formatter stringFromDate:date];
NSLog(@"%@", dateString);

如果要将日期存储为RFC 3339 / ISO 8601格式的字符串,可以使用在plist(或者,如果您需要将 NSDate 转换为字符串以传输到您的Web服务)。如上所述,默认的plist格式不保留 NSDate 对象的秒数,所以如果这很重要,将日期存储为上述代码生成的字符串可能是有用的。

You can use this if you want to store the date as a string in RFC 3339/ISO 8601 format in the plist (or alternatively, if you need to convert a NSDate to a string for transmission to your web service). As noted above, the default plist format does not preserve fractions of a second for NSDate objects, so if that's critical, storing dates as strings as generated by the above code can be useful.

此日期格式化程序可用于将日期转换为字符串(使用 stringFromDate ),以及正确转换格式化字符串到 NSDate 对象(使用 dateFromString )。

And this date formatter can be used for converting dates to strings (using stringFromDate), as well as converting properly formatting strings to NSDate objects (using dateFromString).

这篇关于这些日期是什么类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆