Ios NSDate给出了错误的年份
我试图与NSDate合作,但它对我不起作用,我现在很困惑。所以我想要的基本东西是可行的,但它必须是今天,而不是从2000年开始 所以我想要的是,日期应该是今天的日期和我字符串bU和eU中的小时,而不是2000年的日期和我字符串中的小时 这是我的一段代码:Ios NSDate给出了错误的年份,ios,objective-c,nsdate,Ios,Objective C,Nsdate,我试图与NSDate合作,但它对我不起作用,我现在很困惑。所以我想要的基本东西是可行的,但它必须是今天,而不是从2000年开始 所以我想要的是,日期应该是今天的日期和我字符串bU和eU中的小时,而不是2000年的日期和我字符串中的小时 这是我的一段代码: NSDate *localNotDate; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setTimeZone:[NSTimeZone localTimeZone]];
NSDate *localNotDate;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:@"HH:mm"];
NSDate *bU = [df dateFromString:[[[self alarmArray] objectAtIndex:arrayInt] beginUur]]; // This is a 24:00 format string
NSDate *eU = [df dateFromString:[[[self alarmArray] objectAtIndex:arrayInt] eindUur]]; // This is a 24:00 format string
if (intForInsideForLoop == 0) { // Setup NSDate on first loop.
localNotDate = bU; // This gives the date 2000-01-01 08:24
}
if (intForInsideForLoop < timesOnDay) {
localNotDate = [localNotDate dateByAddingTimeInterval:(interval * 60)]; // Adds minutes to the date of today.
NSDateFormatter *dtest = [[NSDateFormatter alloc] init];
[dtest setTimeZone:[NSTimeZone localTimeZone]];
[dtest setDateFormat:@"yyyy-MM-dd HH:mm"];
NSLog(@"%@", [dtest stringFromDate:localNotDate]); // This gives the date 2000-01-01 08:24. it should be like It's Today thursday so 2014-05-08 08:24
}
NSDate*localNotDate;
NSDateFormatter*df=[[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:@“HH:mm”];
NSDate*bU=[df DATEFORMSTRING:[[[self-alarmArray]objectAtIndex:arrayInt]beginUur]];//这是一个24:00格式的字符串
NSDate*eU=[df DATEFORMSTRING:[[[self-alarmArray]objectAtIndex:arrayInt]einduR]];//这是一个24:00格式的字符串
如果(intForInsideForLoop==0){//Setup NSDate在第一个循环上。
localNotDate=bU;//这给出了日期2000-01-01 08:24
}
if(intForInsideForLoop
我用马丁在帖子中给出的方法找到了问题的答案。此方法将类似(18:00)的“时间”字符串转换为今天18:00的日期
这是:
- (NSDate *)todaysDateFromString:(NSString *)time
{
// Split hour/minute into separate strings:
NSArray *array = [time componentsSeparatedByString:@":"];
// Get year/month/day from today:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
// Set hour/minute from the given input:
[comp setHour:[array[0] integerValue]];
[comp setMinute:[array[1] integerValue]];
return [cal dateFromComponents:comp];
}
也许你可以使用的第一部分。谢谢它的工作的人!