使用 NSDateFormatter 管理日期
我尝试使用 NSDateFormatter 获取和管理日期,但如何使其工作。 我认为这是我的第一个“dateFormatter”的问题,它没有正确的格式,因为当我使用带有函数“stringFromDate”和参数“[NSDate date]”的第二个格式化程序时,它工作得很好,但当我使用我的 NSDate 时则不然日期从字符串”。
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];
//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];
感谢您的帮助 :)
I try to get and manage date with NSDateFormatter but how to make it works.
I think it's a problem with my first "dateFormatter" which have not the right format, because when I use the second formatter with function "stringFromDate" and paramter "[NSDate date]" it works well, but not when I use my NSDate "dateFromString".
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];
//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];
Thank you for your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,DateFormat 与您从服务器接收到的 DateFormat 不同。
试试这个:
Well of course, the DateFormat is not the same as the one you are receiving from the server.
Try this:
假设
to-date
是一个字符串,并且还不是 NSDate,请尝试以下操作:如果您想向用户显示以特定格式格式化的日期,则可以使用设置一个
NSDateFormatter
并使用stringFromDate:
获取要显示给用户的字符串。如果它仍然无法正确显示,请设置日历(我忘记是否始终需要设置它或是否有默认值)。Assuming that
to-date
is a string and not already an NSDate try this instead:If you want to display a date to the user formatted in a particular format you can then use set up an
NSDateFormatter
and usestringFromDate:
to get a string to show the user. If it still doesn't display correctly set the calendar (I forget if you always have to set it or if there is a default).