[英]NSDateFormatter Does not convert correctly
I have a NSString which is passed from an xml feed........ 我有一个从XML提要传递的NSString .......
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. 我的要求是使用NSDateFormatter仅从上述字符串中获得小时部分。 How can I achieve that.
我该如何实现。 Sorry for the earlier mistake.
对不起,先前的错误。
Thanks. 谢谢。
If you want to get the 10 of 10:30 (if its ur requirement) then you can do it like: 如果您想获得10:30中的10(如果您有要求),则可以这样做:
strDate = @"Thu, 22 Apr 2010 10:30 am CEST";
NSArray *dateComponents = [strDate componentsSeparatedByString:@" "];
NSString *requiredString = [dateComponents objectAtIndex:4];
dateComponents = [requiredString componentsSeparatedByString:@":"];
requiredString = [dateComponents objectAtIndex:0];
and when you do: 当您这样做时:
NSLog(rquiredString);
Output : 10; 输出: 10;
This is just a workaround, for better approach you should go through the NSDateComponents class. 这只是一个解决方法,要获得更好的方法,您应该遍历NSDateComponents类。
Hope this helps. 希望这可以帮助。
Thanks, 谢谢,
Madhup 马杜普
You want to do this: 您想这样做:
NSString *strDate =@"Thu, 22 Apr 2010 10:30 am CEST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm a"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:@"hh"];
strDate = [dateFormatter stringFromDate:myDate];
NSLog(@"%@", strDate);
(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 其次,您想忽略CEST位,因为这将导致您的时区被更改
change to 改成
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh.mm a vvvv"];
(. instead of : in hh:mm) (。代替:在hh:mm中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.