【发布时间】:2016-07-12 13:17:19
【问题描述】:
我有以下代码。这里我尝试了两种方法来获取日期和时间。如果我使用格式化程序,它会给我上述错误。如果我不使用它,日期格式不正确,加上我得到的时间也不正确。请帮忙。
NSDate *pickupHoursLate;
NSDate *dropHoursLate;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
NSLog(@"Old Date and Time : %@",[formatter stringFromDate:[NSDate date]]);
这给了我这样的输出 12-07-2016 18:32 这是我想要的
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
int pickHoursToAdd = [[[NSUserDefaults standardUserDefaults] objectForKey:@"pickupPriorTimePeriod"] intValue];
[components setHour:pickHoursToAdd];
pickupHoursLate = [calender dateByAddingComponents:components toDate:[formatter stringFromDate:[NSDate date]] options:0];
这条线给了我错误
int dropHoursToAdd = [[[NSUserDefaults standardUserDefaults] objectForKey:@"dropPriorTimePeriod"] intValue];
[components setHour:dropHoursToAdd];
dropHoursLate = [calender dateByAddingComponents:components toDate:[NSDate date] options:0];
在当前时间加上 2 小时后,这条线给我的值 2016-07-12 15:02:51 +0000 但这里的时间不正确
【问题讨论】:
标签: ios objective-c nsdate