NSDateFormatter 不会在 NSDate 中转换我的 NSString

发布于 2024-12-19 23:54:25 字数 1231 浏览 3 评论 0原文

我有一个字符串,需要将其转换为日期,但它无法正确转换。我不知道为什么......我的代码是:

NSString * fecha = @"2011-12-07 11:11:29.657";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.ZZZ"];

NSDate *dateFromString = [[NSDate alloc] init]; 
dateFromString = [dateFormatter dateFromString:fecha]; 

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *components = [calendar components:units              
                                           fromDate:dateFromString];
 NSInteger* hour = [components hour];
 NSInteger* minute = [components minute];
 NSInteger* second = [components second];  

NSLog(@"Fecha: %@",fecha);
NSLog(@"Format: %@",dateFormatter.dateFormat);
NSLog(@"date: %@",dateFromString);

在我的日志中:

2011-12-07 11:36:11.750 Catalogo-V1[13741:207] Fecha: 2011-12-07 11:11:29.657
2011-12-07 11:36:11.750 Catalogo-V1[13741:207] Format: yyyy-MM-dd HH:mm:ss.ZZZ
2011-12-07 11:36:11.751 Catalogo-V1[13741:207] date: (null)

I have a string and need convert it to a date, but it doesn't convert correctly. I don't know why... my code is:

NSString * fecha = @"2011-12-07 11:11:29.657";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.ZZZ"];

NSDate *dateFromString = [[NSDate alloc] init]; 
dateFromString = [dateFormatter dateFromString:fecha]; 

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *components = [calendar components:units              
                                           fromDate:dateFromString];
 NSInteger* hour = [components hour];
 NSInteger* minute = [components minute];
 NSInteger* second = [components second];  

NSLog(@"Fecha: %@",fecha);
NSLog(@"Format: %@",dateFormatter.dateFormat);
NSLog(@"date: %@",dateFromString);

In my log:

2011-12-07 11:36:11.750 Catalogo-V1[13741:207] Fecha: 2011-12-07 11:11:29.657
2011-12-07 11:36:11.750 Catalogo-V1[13741:207] Format: yyyy-MM-dd HH:mm:ss.ZZZ
2011-12-07 11:36:11.751 Catalogo-V1[13741:207] date: (null)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-12-26 23:54:25

我认为您需要 yyyy-MM-dd HH:mm:ss.SSS 而不是格式字符串 yyyy-MM-dd HH:mm:ss.ZZZ

ZZZ 是时区值,而 SSS 是秒的小数部分。

请参阅 Unicode 文档(链接自 Apple 文档:数据格式

编辑:另一件事:您不需要行 NSDate *dateFromString = [[NSDate alloc] init];。只需调用 NSDate *dateFromString = [dateFormatter dateFromString:fecha]; 即可。日期格式化程序将根据需要分配日期对象。

I think that instead of the format string yyyy-MM-dd HH:mm:ss.ZZZ, you will want yyyy-MM-dd HH:mm:ss.SSS.

ZZZ will be a timezone value, whereas SSS is fractional seconds.

See the Unicode documentation (linked from Apple's docs on Data Formatting)

Edit: one other thing: you don't need to have the line NSDate *dateFromString = [[NSDate alloc] init];. Just call NSDate *dateFromString = [dateFormatter dateFromString:fecha];. The date formatter will allocate the date object as it needs to.

む无字情书 2024-12-26 23:54:25

ZZZ 是一种时区格式,它寻找类似 CET 的内容,而不是秒的小数部分。您想要 SSS

@"yyyy-MM-dd HH:mm:ss.SSS"

http://unicode.org/reports /tr35/tr35-10.html#Date_Format_Patterns

ZZZ is a time zone format, it's looking for something like CET not the fractional part of the seconds. You want SSS

@"yyyy-MM-dd HH:mm:ss.SSS"

http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文