如何将日期字符串(如"2011-06-25T11:00:26 + 01:00")转换为类似于iphone的字符串? [英] How to convert a date string like “2011-06-25T11:00:26+01:00” to a long like iphone?

查看:52
本文介绍了如何将日期字符串(如"2011-06-25T11:00:26 + 01:00")转换为类似于iphone的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将此字符串日期"2011-06-25T11:00:26 + 01:00"转换为类似long的值.

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

我尝试过

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.

谢谢.

推荐答案

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

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".

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

Once you've the string in that format, use the date format – "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);

这篇关于如何将日期字符串(如"2011-06-25T11:00:26 + 01:00")转换为类似于iphone的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆