Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Date Initialisation not working as expected

Sam10020Sam10020 USPosts: 1New Users Noob
edited January 2015 in iOS SDK Development
I am need to compare two dates without time Below is the code
after I initialised it , the date gets reduced by 1 day , not sure whats happening

-(NSInteger )comparedateWithOutTime:(NSDate *)datDate{
NSInteger dateCounter;
dateCounter =0;
if( datDate == nil ) {

datDate = [NSDate date];
NSLog(@Date before compare %@", datDate);

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];

[comps setHour:00];
[comps setMinute:00];
[comps setSecond:00];
[comps setTimeZone:[NSTimeZone localTimeZone]];

datDate = [calendar dateFromComponents:comps];

NSLog(@Date before compare and after intialize %@", datDate);

NSDateComponents* comps2 = NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:[NSDate date;

[comps2 setHour:00];
[comps2 setMinute:00];
[comps2 setSecond:00];
[comps2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateTemp = [[NSCalendar currentCalendar] dateFromComponents:comps2];
//check for today
NSLog(@Date to compare %@",dateTemp);

Compare the dates and return the value


Now the out put is
Click2List[428:60b] Date before compare 2015-01-13 00:00:00 +0000
Click2List[428:60b] Date before compare and after intialize 2015-01-12 06:00:00 +0000
Click2List[428:60b] Date to compare 2015-01-15 00:00:00 +0000

Unable to pin point the error

Post edited by Sam10020 on


  • ryantcbryantcb Posts: 667Registered Users @ @ @
    edited January 2015
    First you are not returning anything when you should be returning an NSInteger. Second you may be best served to future proof any code that you are currently writing by not using iOS 8 deprecated properties. They all now start with NSCalendar...

    it may be the timezone differences?
    Post edited by ryantcb on
Sign In or Register to comment.