如何使用 NSDateFormatter 将 NSString 转换为 NSDate? [英] How to convert NSString to NSDate using NSDateFormatter?

查看:45
本文介绍了如何使用 NSDateFormatter 将 NSString 转换为 NSDate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网络服务中获取日期,例如2012-07-03 07:26:48".从 webservice 解析后,我保存在 NSString 中.将 NSString 转换为下面代码中使用的 NSDate 时,

I getting the date from the webservice like "2012-07-03 07:26:48". I have saved in NSString after parsing from webservice. While convert the NSString to NSDate used below code,

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    NSString *dateStr = @"2012-07-03 07:26:48";
    NSDate *date = [dateFormatter1 dateFromString:dateStr];
    NSLog(@"Date : %@", date);
   The NSLog value is "2012-07-03 01:56:48 +0000";

NSLog 值为2012-07-03 01:56:48 +0000";不知道为什么时间变了.任何人都可以帮我做到这一点吗?

The NSLog value is "2012-07-03 01:56:48 +0000"; I don't know why the time has changed. Can anyone please help me to do this?

推荐答案

你需要把时区设为:

theDateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

因为它正在考虑 GMT 时间格式.

Because it is considering GMT time format.

这篇关于如何使用 NSDateFormatter 将 NSString 转换为 NSDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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