-->

sethours in NSDateComponents

2019-05-10 19:14发布

问题:

I'm trying to sethours in NSDateComponents, I wrote the following code

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];

[dateComps setDay:intDay];
[dateComps setMonth:intMonth];
[dateComps setYear:intYear];
[dateComps setHour:intHoures];
[dateComps setMinute:intMinutes];

NSDate *itemDate;
itemDate = [calendar dateFromComponents:dateComps];

NSLog(@"reminder date: %@", itemDate);

but when I set the hours as 13, it sets it as 11. I want hours in 24 style. Anyone can help me in this issue?

Thanks in Advance.

回答1:

Time difference in the hours indicate that you might have problem with time zone, you could try to set the timezone for the date, I know NSDateFormatter has timeZone Property,I doubt if NSDateComponents have timeZone property. However you could set the timeZone for calendar

[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];