Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ishaq/1eef9f62757611b47ec4 to your computer and use it in GitHub Desktop.
Save ishaq/1eef9f62757611b47ec4 to your computer and use it in GitHub Desktop.
category to remove fractional seconds from an NSDate
#import <Foundation/Foundation.h>
@interface NSDate (dateByRemovingFractionalSeconds)
+ (NSDate *)dateWithoutFractionalSeconds;
- (NSDate *)dateByRemovingFractionalSeconds;
@end
#import "NSDate+dateByRemovingFractionalSeconds.h"
@implementation NSDate (dateByRemovingFractionalSeconds)
+ (NSDate *)dateWithoutFractionalSeconds
{
NSDate *now = [NSDate date];
NSTimeInterval seconds = trunc([now timeIntervalSinceReferenceDate]);
NSDate *date = [[self class] dateWithTimeIntervalSinceReferenceDate:seconds];
return date;
}
- (NSDate *)dateByRemovingFractionalSeconds
{
NSTimeInterval seconds = trunc([self timeIntervalSinceReferenceDate]);
NSDate *date = [[self class] dateWithTimeIntervalSinceReferenceDate:seconds];
return date;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment