sethours in NSDateComponents

巧了我就是萌 提交于 2019-12-22 11:27:58

问题


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"]];


来源:https://stackoverflow.com/questions/15371610/sethours-in-nsdatecomponents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!