Why is this giving me the wrong date ?
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[components setYear:2013];
[components setMonth:06];
[components setDay:28];
[components setHour:5];
[components setMinute:00];
NSDate *startDate1 = [myCalendar dateFromComponents:components];
NSLog(@"start date is %@",startDate1);
start date is 2013-06-28 03:00:00 +0000
EDIT
What I want to do is the following. I have a start and endate.
For example:
start date is 2013-06-28 05:00
end date is : 2013-06-29 04:59
Then I want to check if the current date is between start and end date. I am using the following.
NSComparisonResult result1 = [now compare:startDate1];
NSComparisonResult result2 = [now compare:endDate1];
if (result2 == NSOrderedSame && result1 == NSOrderedSame ) {
NSLOG(@"OKE!");
}
That's because you have to print the date using device's time zone, otherwise it's shown in UTC.
Probably the date is correct, but you misunderstood the log:
Logging a date is always done in TZ +0000. For example, if you are in central europe, you will have the (expected?) date 2013-06-28 05:00:00 +0200, but the log will display the normilzed date 2013-06-28 03:00:00 +0000. This is the same date and time! It is simply expressed in a different way.
+++
If your components are in TZ +0000, too, you should set the time zone of the calendar.
You can check whether the current date is between two dates like this:
Your code actually checks whether the tested date is EQUAL (NSOrderedSame) to both start and end dates (which is not of course)
See my extended example:
This prints out this to the console: