0

为什么这给了我错误的日期?

 NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    [components setYear:2013];
    [components setMonth:06];
    [components setDay:28];
    [components setHour:5];
    [components setMinute:00];
    NSDate *startDate1 = [myCalendar dateFromComponents:components];
    NSLog(@"start date is %@",startDate1);

开始日期是 2013-06-28 03:00:00 +0000

编辑

我想做的是以下。我有一个开始和结束。

例如:

开始日期是 2013-06-28 05:00

结束日期是:2013-06-29 04:59

然后我想检查当前日期是否在开始日期和结束日期之间。我正在使用以下内容。

NSComparisonResult result1 = [now compare:startDate1];
    NSComparisonResult result2 = [now compare:endDate1];

if (result2 == NSOrderedSame && result1 == NSOrderedSame ) {
        NSLOG(@"OKE!");
 }
4

3 回答 3

3

可能日期是正确的,但您误解了日志:

记录日期总是在 TZ +0000 中完成。例如,如果您在中欧,您将有(预期?)日期 2013-06-28 05:00:00 +0200,但日志将显示标准化日期 2013-06-28 03:00:00 +0000。这是相同的日期和时间!它只是以不同的方式表达。

+++

如果您的组件也在 TZ +0000 中,您应该设置日历的时区。

于 2013-05-13T11:14:55.683 回答
1

您可以检查当前日期是否在两个日期之间,如下所示:

NSDate *now = [NSDate date];
BOOL betweenStartAndEnd = ([startDate compare:now] == NSOrderedAscending && [endDate compare:now] == NSOrderedDescending);

您的代码实际上检查测试日期是否与开始日期和结束日期相等(NSOrderedSame)(当然不是)

result2 == NSOrderedSame && result1 == NSOrderedSame

请参阅我的扩展示例:

NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[components setYear:2013];
[components setMonth:05];
[components setDay:13];
[components setHour:18];
[components setMinute:00];
NSDate *startDate = [myCalendar dateFromComponents:components];
[components setDay:15];
NSDate *endDate = [myCalendar dateFromComponents:components];

NSDate *now = [NSDate date];
BOOL betweenStartAndEnd = ([startDate compare:now] == NSOrderedAscending && [endDate compare:now] == NSOrderedDescending);

NSLog(@"Date %@ %@ between %@ and %@", now, betweenStartAndEnd ? @"IS" : @"IS NOT", startDate, endDate);

这会将其打印到控制台:

Date 2013-05-13 15:44:06 +0000 IS NOT between 2013-05-13 16:00:00 +0000 and 2013-05-15 16:00:00 +0000
于 2013-05-13T15:56:32.203 回答
0

那是因为您必须使用设备的时区打印日期,否则它会以 UTC 显示。

NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    [components setYear:2013];
    [components setMonth:06];
    [components setDay:28];
    [components setHour:5];
    [components setMinute:00];
    NSDate *startDate1 = [myCalendar dateFromComponents:components];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone systemTimeZone];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSLog(@"%@", [formatter stringFromDate:startDate1]);
于 2013-05-13T11:18:04.820 回答