NSDate 格式化程序转换未产生正确的日期
我面临使用日期格式化程序正确检索日期的问题。我已将日期保存为以下格式:
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
现在我需要在视图控制器中显示保存的日期。为了方便可读性和简单性,我从数据库中检索了保存的日期并更改为短格式,例如1月17日,2月18日等。这是其实现代码:
remind.Date = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 3)];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:remind.Date];
[dateFormat setDateFormat:@"MMMM dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remind.Date = dateVal;
现在日期值将可见,如以下快照所示:
所有这些日期都保存在到一个名为grpArray
现在我想检索格式化日期的完整日期,当我选择行并导航到保存该日期的控制器时,为此在索引路径中选择行我实现了以下代码:
-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReminderClass *rem = [self.grpArray objectAtIndex:indexPath.section];
// Instantiate your detail/editor view controller,
// and pass in the ReminderClass object to be edited.
ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"MMMM dd"];
[dateFormat setLenient:YES];
NSDate *date = [dateFormat dateFromString:rem.Date];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateVal = [dateFormat stringFromDate:date];
rem.Date = dateVal;
[self.navigationController pushViewController:rdvc animated:YES];
rdvc.navigationItem.rightBarButtonItem.title = @"Edit";
}
现在根据我应该的逻辑获取指定的完整日期 ie 格式。它工作正常,但在最初保存的正确年份和时间方面出现问题。请参阅以下快照以进行说明:
现在我们看到的日期是 1970-01-17 00:00:00
但我保存的实际日期是 2012-01-17 19:40:40
出了什么问题 提前
任何人都可以正确指导我吗?
谢谢大家:)
I am facing issue with proper date retrieval using date formatter.I have saved the date in the following format:
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
Now I need to display the saved date in view controller.For the convenience of readability and simplicity,I have retrieved the saved date from database and changed to a short format say January 17,February 18 etc..Here is the implementation code for that:
remind.Date = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 3)];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:remind.Date];
[dateFormat setDateFormat:@"MMMM dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remind.Date = dateVal;
Now the date value will be visible as shown in the following snap shot:
All these dates are saved in to an array called grpArray
Now I want to retrieve the complete date of formatted date,when I select the row and navigate to controller where that date is saved,for that in did select row at index path I implemented the following code:
-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReminderClass *rem = [self.grpArray objectAtIndex:indexPath.section];
// Instantiate your detail/editor view controller,
// and pass in the ReminderClass object to be edited.
ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"MMMM dd"];
[dateFormat setLenient:YES];
NSDate *date = [dateFormat dateFromString:rem.Date];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateVal = [dateFormat stringFromDate:date];
rem.Date = dateVal;
[self.navigationController pushViewController:rdvc animated:YES];
rdvc.navigationItem.rightBarButtonItem.title = @"Edit";
}
Now according to the logic I should get the complete date i.e. format specified.Its working fine but got a problem with correct year and time that was originally saved.Please see the following snap shot for clarification:
Now the date as we can see is 1970-01-17 00:00:00
But the actual date which I saved was 2012-01-17 19:40:40
What went wrong with the implementation
Can any one please guide me right
Thanks all in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的是您正在尝试将日期
MMMM dd
转换为yyyy-MM-dd HH:mm:ss
。这当然是不可能的,因为第一种格式的日期不知道年份和时间。
这就是为什么它显示 1970 00:00:00 ,01-17 是正确的(当然)
你总是必须“存储”原始日期
yyyy-MM-dd HH:mm:ss< /code> 并使用它而不是格式化的。
What I see is you're trying to convert a date
MMMM dd
to ayyyy-MM-dd HH:mm:ss
.Which ofcourse isn't possible since the date in the first format doesn't know the year and time.
That's why it's showing 1970 00:00:00 , the 01-17 are correct (ofcourse)
You always have to 'store' the original date
yyyy-MM-dd HH:mm:ss
and work with that instead of the formatted one.