Created
October 3, 2012 08:53
-
-
Save trevorsheridan/3825877 to your computer and use it in GitHub Desktop.
Strips the time from a date with respect to the user's timezone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDate+Additions.m | |
// | |
// Created by Trevor Sheridan on 10/1/12. | |
// Copyright (c) 2012 Trevor Sheridan Inc. All rights reserved. | |
// | |
#import "NSDate+Additions.h" | |
@implementation NSDate (Additions) | |
- (NSDate *)dateWithoutTime | |
{ | |
return [[self class] dateWithoutTime:self]; | |
} | |
- (NSDate *)dateInCurrentTimezone | |
{ | |
return [[self class] dateInCurrentTimeZone:self]; | |
} | |
// Return the date without time in the user's current time zone. | |
+ (NSDate *)dateWithoutTime:(NSDate *)date | |
{ | |
date = [self dateInCurrentTimeZone:date]; | |
NSCalendar *calendar = [NSCalendar currentCalendar]; | |
NSDateComponents *components = [calendar components:NSTimeZoneCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSSecondCalendarUnit fromDate:date]; | |
/* If date's time is set to 06:00:00 in the Mountain Time Zone, simply set the seconds to the | |
offset. So -21600 seconds + 06:00:00 = 00:00:00. This should work for all timezones. | |
*/ | |
components.second = [components.timeZone secondsFromGMT]; | |
return [calendar dateFromComponents:components]; | |
} | |
// Given a UTC date (GMT offset 0) return a new date to midnight in the user's current timezone. | |
+ (NSDate *)dateInCurrentTimeZone:(NSDate *)date | |
{ | |
NSCalendar *calendar = [NSCalendar currentCalendar]; | |
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date]; | |
return [calendar dateFromComponents:components]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment