-[NSDateFormatter dateFromString:] 返回 nil

发布于 2024-11-09 11:20:30 字数 1375 浏览 0 评论 0原文

    NSString *lower = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:0]];
    NSString *higher = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:[newDates count]-1]];
    NSLog(@"%@",lower);
    NSLog(@"%@",higher);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *dtLower = [df dateFromString:lower];

    NSDate *dtHigher = [df dateFromString:higher];
    [df release];
    NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
    [df1 setDateFormat:@"yyyy-MM-dd"];
    NSString * lowerDate = [df1 stringFromDate:dtLower];
    NSString * higherDate = [df1 stringFromDate:dtHigher];
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 31, self.view.frame.size.width, 30)];
    [dateLabel setTextColor:[UIColor grayColor]];
    dateLabel.text = [NSString stringWithFormat:@"%@ - %@",lowerDate,higherDate];
    [self.view addSubview:dateLabel];

控制台中 lowerhigher 的输出是:

2011-05-23 14:55:52.767 Vocab[4225:207] 2011-05-23 03:58:22
2011-05-23 14:55:53.781 Vocab[4225:207] 2011-05-23 07:14:56

这里上面的代码对我不起作用。 higherlower 的值不为空,但是当我尝试使用 NSDateFormatter 将其转换为 NSDate 时,它​​返回 nil。

所以 dtLowerdtHigher 返回 nil

可能出了什么问题?

    NSString *lower = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:0]];
    NSString *higher = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:[newDates count]-1]];
    NSLog(@"%@",lower);
    NSLog(@"%@",higher);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *dtLower = [df dateFromString:lower];

    NSDate *dtHigher = [df dateFromString:higher];
    [df release];
    NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
    [df1 setDateFormat:@"yyyy-MM-dd"];
    NSString * lowerDate = [df1 stringFromDate:dtLower];
    NSString * higherDate = [df1 stringFromDate:dtHigher];
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 31, self.view.frame.size.width, 30)];
    [dateLabel setTextColor:[UIColor grayColor]];
    dateLabel.text = [NSString stringWithFormat:@"%@ - %@",lowerDate,higherDate];
    [self.view addSubview:dateLabel];

Output of lower and higher in Console is:

2011-05-23 14:55:52.767 Vocab[4225:207] 2011-05-23 03:58:22
2011-05-23 14:55:53.781 Vocab[4225:207] 2011-05-23 07:14:56

Here the above code does not work for me. The value for higher and lower are not null but when I try converting it into NSDate using NSDateFormatter then it returns nil.

So dtLower and dtHigher return nil

What could be wrong?

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

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

发布评论

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

评论(3

蓝颜夕 2024-11-16 11:20:30

尝试设置 [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

Try setting [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

愛放△進行李 2024-11-16 11:20:30
 [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

您的第一个日期格式错误。

 [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

Your first date format is wrong.

墨落成白 2024-11-16 11:20:30

如果您使用用户可见的日期,则应避免设置日期格式字符串,因为很难预测格式字符串在所有可能的用户配置中的表达方式。相反,您应该尝试限制自己设置日期和时间样式(通过 -[NSDateFormatter setDateStyle:]-[NSDateFormatter setTimeStyle:])

如果您使用固定格式日期,则应首先将日期格式化程序的区域设置设置为适合您的固定格式的区域设置。在大多数情况下,最好选择的区域设置是 en_US_POSIX,该区域设置是专门为生成美国英语结果而设计的,无论用户和系统首选项如何。 en_US_POSIX 在时间和机器之间也是不变的(en_US_POSIX 在 iOS 上的工作方式与在 OSX 上的工作方式相同,以及在其他平台上的工作方式相同)。

en_US_POSIX 设置为日期格式化程序的区域设置后,您就可以设置日期格式字符串,并且日期格式化程序将对所有用户保持一致的行为。

上述信息及更多信息可以在Apple的技术问答QA1480<中找到/a>

这是实现上述技术说明中建议的代码片段:

static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter) 
{
   dateFormatter = [NSDateFormatter new];

   NSLocale *enUSPOSIXLocale = [[NSLocale alloc]  
                                 initWithLocaleIdentifier:@"en_US_POSIX"];

   [dateFormatter setLocale:enUSPOSIXLocale];
   [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000";
}

If you're working with user-visible dates, you should avoid setting a date format string as it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should try and limit yourself to setting date and time styles (via -[NSDateFormatter setDateStyle:] and -[NSDateFormatter setTimeStyle:]).

If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences. en_US_POSIX is also invariant in time and between machines (en_US_POSIX works the same on iOS as it does on OSX, and as it it does on other platforms).

Once you've set en_US_POSIX as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.

The above info and more can be found in Apple's Technical Q&A QA1480

Here's a snippet of code which implements the recommendation from the above Technical Note:

static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter) 
{
   dateFormatter = [NSDateFormatter new];

   NSLocale *enUSPOSIXLocale = [[NSLocale alloc]  
                                 initWithLocaleIdentifier:@"en_US_POSIX"];

   [dateFormatter setLocale:enUSPOSIXLocale];
   [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文