将 NSString 解析为 NSDate 时出现问题

发布于 2024-10-29 15:58:46 字数 453 浏览 3 评论 0原文

我有具有此值的 XML:

<LastModifiedOnDate>2011-04-02T00:00:00</LastModifiedOnDate>

我试图在 iPhone 上将其从 NSString 解析为 NSDate 但没有运气。

NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];  
        [formate setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];  
        //NSString *strConcat = @"2010-09-24 17:30:00";  
        NSDate *date = [formate dateFromString:string];

I have XML with this value:

<LastModifiedOnDate>2011-04-02T00:00:00</LastModifiedOnDate>

I am trying to parse this on iPhone from NSString to NSDate but have no luck.

NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];  
        [formate setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];  
        //NSString *strConcat = @"2010-09-24 17:30:00";  
        NSDate *date = [formate dateFromString:string];

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

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

发布评论

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

评论(3

时光与爱终年不遇 2024-11-05 15:58:46

您不需要格式字符串中的所有这些撇号。您唯一需要的是 T 周围的值。

NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];  
[formate setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
// I recommend setting a timezone because it will use the system timezone and that
// can cause confusion with your data.  
// [formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *strConcat = @"2010-09-24T17:30:00";  
NSDate *date = [formate dateFromString:strConcat];
NSLog(@"Date: %@", date);  // remember NSDate default to current timezone when logging to console

如果您的字符串有秒的一小部分,则将 SSS 添加到您的字符串格式中: [formate setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS "];

有关日期格式的更多信息,请查看 Unicode 技术标准 # 35

You don't need all those apostrophes in your format string. The only ones you need are around the T.

NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];  
[formate setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
// I recommend setting a timezone because it will use the system timezone and that
// can cause confusion with your data.  
// [formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *strConcat = @"2010-09-24T17:30:00";  
NSDate *date = [formate dateFromString:strConcat];
NSLog(@"Date: %@", date);  // remember NSDate default to current timezone when logging to console

If you string have fraction of seconds, then add SSS to your string format: [formate setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];

For additional information on date formatting check out Unicode Technical Standard #35.

强辩 2024-11-05 15:58:46

将您的 SetDataFormat 函数替换为下面的函数,并让我知道结果...

    [formate setDateFormat:@"yyyy-MM-ddTHH:mm:ss"];

Replace your's SetDataFormat function with below and let me know for the result ...

    [formate setDateFormat:@"yyyy-MM-ddTHH:mm:ss"];
绅士风度i 2024-11-05 15:58:46
NSString *string=@"2011-04-05T12:43:56.537";
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];  
[formate setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];  
NSDate *date = [formate dateFromString:string];

它的工作..

NSString *string=@"2011-04-05T12:43:56.537";
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];  
[formate setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];  
NSDate *date = [formate dateFromString:string];

its working..

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