NSDateFormatter 无法正确转换

发布于 2024-08-30 00:42:23 字数 792 浏览 2 评论 0原文

我有一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懒的傷心 2024-09-06 00:42:23

如果你想得到 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 类。

希望这会有所帮助。

谢谢,

马杜普

If you want to get the 10 of 10:30 (if its ur requirement) then you can do it like:

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;

This is just a workaround, for better approach you should go through the NSDateComponents class.

Hope this helps.

Thanks,

Madhup

没︽人懂的悲伤 2024-09-06 00:42:23

更改为

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

(. 而不是 hh:mm 中的 :)

change to

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

(. instead of : in hh:mm)

番薯 2024-09-06 00:42:23

你想要这样做:(

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位,因为这会导致你的时区被更改

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文