如何将带时区的日期字符串转换为NSDate [英] How to convert the Date String with time zone to NSDate

查看:79
本文介绍了如何将带时区的日期字符串转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将日期字符串"2012-05-03 06:03:00 +0000"转换为NSDate.我使用了下面的代码,但是它不起作用:

How to convert the Date String "2012-05-03 06:03:00 +0000" to NSDate. I user the code below, but it does not work:

NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate* date = [datFormatter dateFromString:dateStr];

推荐答案

  NSString *dateStr = @"2012-05-03 06:03:00 +0000";
  NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
  [datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
  NSDate* date = [datFormatter dateFromString:dateStr];
  NSLog(@"date: %@", [datFormatter stringFromDate:date]);

此代码打印日期:2012-05-03 09:03:00 +0300

有效.另外,别忘了设置您要定位的日期格式程序的timeZone

It works. Also don't forget to set the date formatter's timeZone that you're targeting

这篇关于如何将带时区的日期字符串转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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