来自UTC日期字符串的NSDate(没有时间)

时间:2014-09-07 22:55:44

标签: nsdate

我已经查看了几乎所有关于NSDate转换的帖子,但是没有成功解决我的问题。

背景: 我想在CalendarView中渲染值时简单地比较NSDate(没有时间)值。但是,当我从服务器转换ISO日期时,它实际上返回上一个日期,因为我猜是GMT偏移值。但是,我只想表彰从服务器传递的年,月,日。

代码

服务器的值:date: "2014-09-26T00:00:00.000Z"

Objective-C代码转换为NSDate。我正在使用Soffes library来处理ISO日期转换。

NSTimeZone *timezone = [NSTimeZone timeZoneWithName:@"UTC"]; NSDate *date = [NSDate dateWithISO8601String:myDate timeZone:&timezone usingCalendar:nil]; NSDate *dateWithoutTime = [NSDate dateWithOutTime:date];

没有时间返回日期的代码:

NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date]; return [[NSCalendar currentCalendar] dateFromComponents:comps];

当前结果

ISO日期来自字符串

po date 2014-09-26 00:00:00 +0000

没有时间的日期

po dateWithoutTime 2014-09-25 06:00:00 +0000

有关我如何仍然可以遵守从服务器传递的年,月,日的任何建议?

0 个答案:

没有答案