0

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.

4

3 回答 3

1

如果您想获得 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];

当你这样做时:

NSLog(rquiredString);

输出: 10;

这只是一种解决方法,为了更好的方法,您应该通过NSDateComponents 类。

希望这可以帮助。

谢谢,

马杜普

于 2010-04-22T10:54:08.233 回答
0

你想这样做:

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);

(首先你的原始格式化程序是错误的,你有一个:而不是一个。)编辑不再是这种情况

其次,您要忽略 CEST 位,因为这会导致您的时区被更改

于 2010-04-22T10:14:59.203 回答
0

改成

[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh.mm a vvvv"];

(。而不是:在 hh:mm 中)

于 2010-04-22T10:15:32.213 回答