如何转换日期字符串,如 “2011-06-25T11:00:26+01:00”要长的像iphone吗?

发布于 2024-11-17 23:07:40 字数 523 浏览 4 评论 0原文

我需要将此字符串日期“2011-06-25T11:00:26+01:00”转换为类似的长值。

我尝试了此操作

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];

[df setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

[df setDateFormat:@"yyyy-MM-ddHH:mm:ssZ"];

NSDate *date = [df dateFromString:[time stringByReplacingOccurrencesOfString:@"T" withString:@""]];

[df setDateFormat:@"eee MMM dd, yyyy hh:mm"];

NSLog(@"time%@", time);
long lgTime =  (long)[date timeIntervalSince1970];

,但这不起作用。请帮助我。

提前致谢。

I need to convert this string date "2011-06-25T11:00:26+01:00” into a long like value.

I tried this

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];

[df setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

[df setDateFormat:@"yyyy-MM-ddHH:mm:ssZ"];

NSDate *date = [df dateFromString:[time stringByReplacingOccurrencesOfString:@"T" withString:@""]];

[df setDateFormat:@"eee MMM dd, yyyy hh:mm"];

NSLog(@"time%@", time);
long lgTime =  (long)[date timeIntervalSince1970];

but this doesn't work. Please help me.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

放赐 2024-11-24 23:07:40

首先,我第一次错过了这个,但是 "2011-06-25T11:00:26+01:00" 无法解析。正确的字符串是 "2011-06 -25T11:00:26+0100”

获得该格式的字符串后,请使用日期格式 - “yyyy-MM-dd'T'HH:mm:ssZ”

用法示例

NSString * time = @"2011-06-25T11:00:26+0100";

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];  
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

NSDate *date = [df dateFromString:time];

long lgTime = (long)[date timeIntervalSince1970];

NSLog(@"%ld", lgTime);

First of all, I missed this the first time but "2011-06-25T11:00:26+01:00” is cannot be parsed. The correct string would be "2011-06-25T11:00:26+0100”.

Once you've the string in that format, use the date format – "yyyy-MM-dd'T'HH:mm:ssZ".

Example usage

NSString * time = @"2011-06-25T11:00:26+0100";

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];  
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

NSDate *date = [df dateFromString:time];

long lgTime = (long)[date timeIntervalSince1970];

NSLog(@"%ld", lgTime);
情独悲 2024-11-24 23:07:40

如果要将“2011-06-25T11:00:26+01:00”转换为“2011-06-25T11:00:26GMT+01:00”可以解析

NSMutableString *string=[[NSMutableString alloc]initWithString:@"2011-06-25T11:00:26+01:00"];

NSRange range = [string rangeOfString:@"+" options:NSBackwardsSearch];
[string insertString:@"GMT" atIndex:range.location];
NSLog(@"string is %@",string);

希望这对你有帮助

If you want to convert "2011-06-25T11:00:26+01:00” to "2011-06-25T11:00:26GMT+01:00" which can be parsed

NSMutableString *string=[[NSMutableString alloc]initWithString:@"2011-06-25T11:00:26+01:00"];

NSRange range = [string rangeOfString:@"+" options:NSBackwardsSearch];
[string insertString:@"GMT" atIndex:range.location];
NSLog(@"string is %@",string);

Hope this helps you

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