如何根据目标c中的工作日确定下一个最近的一天?

时间:2014-04-29 08:44:38

标签: ios objective-c nsdate nsdateformatter

为什么GMT还会显示以下内容?我试图让它显示在EST多伦多,所以我将NSTimeZone设置为localTimeZone

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-dd-MM"];
    NSString *todayDateString = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]
                                                                                 sinceDate:[NSDate date]]];
    [dateFormatter setDateFormat:@"yyyy-dd-MM hh:mm:ss +0000"];

    NSDate *bus500_monday_To_Friday_T1 = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 06:40:00 +0000", todayDateString]]; // 6:40 AM
    NSDate *bus500_monday_To_Friday_T2 = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 07:50:00 +0000", todayDateString]]; // 7:50 AM

    NSLog(@"%@", bus500_monday_To_Friday_T2);

1 个答案:

答案 0 :(得分:1)

如果我理解你今天需要在字符串中使用日期而不是手动更改硬编码字符串,那么你应该使用以下代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"yyyy-dd-MM"];
NSString *todayDateString = [dateFormatter stringFromDate:[NSDate date]];

[dateFormatter setDateFormat:@"yyyy-dd-MM hh:mm:ss +0000"];
NSDate *bus500_monday_To_Friday_T1 = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 06:40:00 +0000", todayDateString]]; // 6:40 AM
NSDate *bus500_monday_To_Friday_T2 = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 07:50:00 +0000", todayDateString]]; // 7:50 AM
//use above code for all other date variables too.

// rest code would be same

在此之后,您可以使用日期比较方法来了解下一个总线时间最接近当前设备时间。