[英]Problems of time zones “ZE8”, How to convert NSDate to NSString
How to convert (12/27/2013 05:49:41 PM ZE8) NSString to NSDate? 怎么转换(ZE12 / 27/2013 05:49:41 PM) NSString到NSDate?
NSString *string = @"12/27/2013 05:49:41 PM ZE8";
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a z"];
NSDate *publishDate = [formatter dateFromString:string];
publishDate is getting null value. publishDate获取空值。 What is issue that? 那是什么问题?
The problem appears because ZE8
format is NOT valid format for timezones in unix systems! 出现问题是因为ZE8
格式对于unix系统中的时区不是有效格式!
You can use: 您可以使用:
But ZE8 is invalid 但是ZE8无效
You have to convert this data to valid one, so you have to add another Array/table/dictionary what do you like with this values http://www.ibm.com/developerworks/lotus/library/ls-keeping_time/side1.html and convert it to RFC 822
for example 您必须将此数据转换为有效的数据,因此您必须添加另一个Array / table / dictionary,并使用此值http://www.ibm.com/developerworks/lotus/library/ls-keeping_time/side1。 html并将其转换为RFC 822
No, there is no built in way - you have to write this structure by yourself. 不,没有内置的方式-您必须自己编写此结构。 For example using dictionary (if i were you i will write a category by myself). 例如使用字典(如果我是我,我会自己写一个类别)。 I write a simple solution for you, of course if you do this often you should keep this dictionary somewhere and also write all the values from above page (i write only two): 我为您编写了一个简单的解决方案,当然,如果您经常这样做,则应将此字典保留在某个位置,并且还要写上一页中的所有值(我只写两个):
-(void)myBaseMethod {
NSDate *finalDate = [self convertStringToDate:@"12/27/2013 05:49:41 PM ZE8"];
}
-(NSDate*)convertStringToDate:(NSString*)str {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a z"];
return [formatter dateFromString:[self changeTimeZoneToValidOneFromString:str]];
}
-(NSString*)changeTimeZoneToValidOneFromString:(NSString*)inputStrDate {
NSDictionary *timeZonesDic = [NSDictionary dictionaryWithObjectsAndKeys:
@"GMT-08:00",@"ZE8",
@"GMT-7:00",@"ZE7",
nil];
NSMutableArray *dateItems = [[inputStrDate componentsSeparatedByString:@" "] mutableCopy]; //in here I break the original string into array of strings (every element is separate date component)
NSString *timeZoneString = [timeZonesDic objectForKey:[dateItems lastObject]]; //convert using dictionary
[dateItems replaceObjectAtIndex:([dateItems count]-1) withObject:timeZoneString]; //replacing the last object of the existing array.
return [dateItems componentsJoinedByString:@" "]; //in here I join it back together
}
Really make a Category
( NSDate
) for it, then you will be call only convertStringToDate:
method and the rest will be in one place. 真正为其创建一个Category
( NSDate
),那么您将只调用convertStringToDate:
方法,其余的将放在一个地方。
Hope this will help you. 希望这会帮助你。
Try 尝试
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
[formatter release];
All the best 祝一切顺利
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.