NSDateFormatter 未返回正确的日期

发布于 2024-10-01 09:10:34 字数 1116 浏览 9 评论 0原文

我有以下代码,用于获取一个 NSDate 的时间和当前日期,并将它们组合成一个 NSDate。然而,格式化程序设置正确,但它返回的日期与应有的日期并不接近。这是代码,

/* Get the current date and make a formatter to just show the date ONLY */
NSDate *currentDate = [NSDate date];
NSDateFormatter *curDateFormatter = [[NSDateFormatter alloc] init];
[curDateFormatter setDateFormat:@"MM/dd/YYYY"];

/* Create a formatter for the time ONLY */
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"hh:mm"];

/* Create a formatter for both date and time */
NSDateFormatter *combinedFormatter = [[NSDateFormatter alloc] init];
[combinedFormatter setDateFormat:@"MM/dd/YYYY hh:mm"];

NSString *combinedDateTime = [NSString stringWithFormat:@"%@ %@", [curDateFormatter stringFromDate:currentDate], [timeFormatter stringFromDate:time]];
NSDate *combinedDate = [combinedFormatter dateFromString:combinedDateTime];

/* release the formatters */
[curDateFormatter release];
[timeFormatter release];
[combinedFormatter release];

return combinedDate;

假设在发布此消息时它正在执行此操作,它应该具有 11/10/2010 06:47,但实际上却是 12/27/2009 11:45。在模拟器和设备中执行此操作。

I have the following code which is used to take the time of one NSDate and the current date and combine them into one NSDate. However the formatters are set correctly, but it's returning a date that isn't even close to the one it should be. Here is the code

/* Get the current date and make a formatter to just show the date ONLY */
NSDate *currentDate = [NSDate date];
NSDateFormatter *curDateFormatter = [[NSDateFormatter alloc] init];
[curDateFormatter setDateFormat:@"MM/dd/YYYY"];

/* Create a formatter for the time ONLY */
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"hh:mm"];

/* Create a formatter for both date and time */
NSDateFormatter *combinedFormatter = [[NSDateFormatter alloc] init];
[combinedFormatter setDateFormat:@"MM/dd/YYYY hh:mm"];

NSString *combinedDateTime = [NSString stringWithFormat:@"%@ %@", [curDateFormatter stringFromDate:currentDate], [timeFormatter stringFromDate:time]];
NSDate *combinedDate = [combinedFormatter dateFromString:combinedDateTime];

/* release the formatters */
[curDateFormatter release];
[timeFormatter release];
[combinedFormatter release];

return combinedDate;

Say it is doing it when this message was posted, it should have 11/10/2010 06:47 but instead it's like 12/27/2009 11:45. Does this in the simulator and the device.

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

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

发布评论

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

评论(2

铁憨憨 2024-10-08 09:10:34

不要使用组合日期格式化程序,而是尝试以下操作:

    NSDate *combinedDate = [NSDate dateWithNaturalLanguageString:combinedDateTime];

这对我来说效果很好。

组合日期时间字符串的值为 09/27/2011 11:55

组合日期日期对象的值为 2011-09-27 11:55:00 +0530

Instead of using the combinedDateFormatter, try the following:

    NSDate *combinedDate = [NSDate dateWithNaturalLanguageString:combinedDateTime];

This worked fine for me.

Value for combinedDateTime string was 09/27/2011 11:55

Value for combinedDate date object was 2011-09-27 11:55:00 +0530

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