NSDateFormatter 无法正确转换
我有一个从 xml feed 传递的 NSString......
NSString *strDate =@"Thu, 22 Apr 2010 10.30 am CEST";
我目前正在使用此代码来格式化日期......
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm a vvvv"];
NSDate *myDate = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:@"HH"];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);
我想格式化我的字符串仅显示小时和目前我正在获得这样的价值。
strDate =@"2010-04-10 14:00:00 +0530";
谁能帮我解决这个问题吗?......
对不起。这是我的错误。应该是这样的。
NSString *strDate =@"Thu, 22 Apr 2010 10:30 am CEST";
我的要求是使用 NSDateFormatter 仅从上面的字符串中获取小时部分。我怎样才能做到这一点。对之前的错误表示歉意。
谢谢。
I have a NSString which is passed from an xml feed........
NSString *strDate =@"Thu, 22 Apr 2010 10.30 am CEST";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm a vvvv"];
NSDate *myDate = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:@"HH"];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display only hours and currently I'm getting value like this.
strDate =@"2010-04-10 14:00:00 +0530";
Can anyone please help me with this?......
I'm sorry.It's my mistake.It should be like this.
NSString *strDate =@"Thu, 22 Apr 2010 10:30 am CEST";
What my requirement is to get hour part only from above string using NSDateFormatter. How can I achieve that. Sorry for the earlier mistake.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想得到 10:30 中的 10(如果这是你的要求)那么你可以这样做:
当你这样做时:
输出: 10;
这只是一种解决方法,为了更好的方法,您应该使用 NSDateComponents 类。
希望这会有所帮助。
谢谢,
马杜普
If you want to get the 10 of 10:30 (if its ur requirement) then you can do it like:
and when you do:
Output : 10;
This is just a workaround, for better approach you should go through the NSDateComponents class.
Hope this helps.
Thanks,
Madhup
更改为
(. 而不是 hh:mm 中的 :)
change to
(. instead of : in hh:mm)
你想要这样做:(
首先你原来的格式化程序是错误的,你有一个:而不是一个。)编辑不再是这种情况
其次,你想要忽略CEST位,因为这会导致你的时区被更改
You want to do this:
(Firstly your original formatter was wrong, you had a: instead of a .) EDIT no longer the case
Secondly, you want to ignore the CEST bit as this will cause your timezone being changed