所以我正在创建一个跟踪每日,每周,每月预算的应用,并坚持使用此代码。它工作,它不起作用,也许我甚至没有向你展示一切正确的部分。有时候它有效。假设我从剩余的24小时开始,并提前1-2天跳过,由于某种原因,它会增加小时数而不是减去小时数。而其他时候它会正常工作。 (罚款我的意思是第二天重置,如果我在第二天一个小时后检查,则还有23个小时)。不介意开始日期,没关系。
NSString *start = @"06-08-2013 12:30:00";
int typeDays = [[NSUserDefaults standardUserDefaults] integerForKey:@"typeKey"];
if (typeDays == 0) {
days = 1;
} else if (typeDays == 1) {
days = 7;
} else if (typeDays == 2) {
days = 30;
}
NSLog(@"typedays:%i", typeDays);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:days];
newDate = [[NSCalendar currentCalendar]
dateByAddingComponents:dateComponents
toDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"origDateKey"] options:0];
self.untilDeparture = [[SKEvent alloc] initWithStartDate:[dateFormatter dateFromString:start]
andEndDate:newDate];
这是我获得小时/天的地方:
NSString *testString = [[NSString alloc] initWithFormat:@"%.0f", [self.untilDeparture.endDate timeIntervalSinceDate:[NSDate date]] / 3600];
testInt = [testString intValue];
NSLog(@"string:%@ and int:%i", testString, testInt);
if (testInt < 1 ) {
NSLog(@"asdasdasd");
[self resetAllData];
[self viewWillAppear:YES];
[self reloadLabel];
}
dateHours = [[NSString alloc] initWithFormat:@"%.0f Hours", [self.untilDeparture.endDate timeIntervalSinceDate:[NSDate date]] / 3600];
dateDays = [[NSString alloc] initWithFormat:@"%.0f Days Left", [self.untilDeparture.endDate timeIntervalSinceDate:[NSDate date]] / 3600 / 24];
if ([dateDays isEqualToString:@"1 Days Left"] || [dateDays isEqualToString:@"0 Days Left"]) {
dateDays = [[NSString alloc] initWithFormat:@"%.0f Hours Left", [self.untilDeparture.endDate timeIntervalSinceDate:[NSDate date]] / 3600];
}
self.daysLeftLabel.text = dateDays;
然后当我重置数据时:
[[NSUserDefaults standardUserDefaults] setObject:newDate forKey:@"origDateKey"];
[[NSUserDefaults standardUserDefaults] synchronize];