将 NSString 转换为 NSDate,并将日期和时间作为不同的字符串接收
我知道这个问题已经被问了很多,但即使在网上进行了大量搜索后,我仍然在转换中遇到问题。有人可以指出我在以下代码中的错误吗?
NSString *eventDttm = [[NSString alloc] init];
eventDttm = [NSString stringWithFormat:@"%@ %@",myEvent.startDate,myEvent.timeFrom];
NSLog(@"eventDttm: %@",eventDttm);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:MM a"];
//conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:eventDttm];
[formatter release];
NSLog(@"%@",dateFromString);
我在字符串中分别获取日期和时间,如下所示:
myEvent.startDate= 2011-11-22
myEvent.timeFrom= 08:00 PM
但 dateFromString 始终为 null
I know this have been asked a lot but I am still having the problem in this conversion even after extensive search on the net. Can some one point me to my mistake in following code?
NSString *eventDttm = [[NSString alloc] init];
eventDttm = [NSString stringWithFormat:@"%@ %@",myEvent.startDate,myEvent.timeFrom];
NSLog(@"eventDttm: %@",eventDttm);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:MM a"];
//conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:eventDttm];
[formatter release];
NSLog(@"%@",dateFromString);
I am getting date and time separately in string as following:
myEvent.startDate= 2011-11-22
myEvent.timeFrom= 08:00 PM
but dateFromString is always null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要混淆月份和分钟 (MM & mm)
Don't mix Months and minutes (MM & mm)