unexpected result of dateFromComponents:

后端 未结 3 1227
清酒与你
清酒与你 2021-01-21 11:51

the following code

NSCalendar *cal = [NSCalendar currentCalendar];
[cal setFirstWeekday:2];
[cal setMinimumDaysInFirstWeek:4];

NSDateComponents *comp = [[NSDate         


        
3条回答
  •  一个人的身影
    2021-01-21 12:19

    I assume you are using the CET time zone locally. This is what the [NSCalendar currentCalendar] will use as a time zone then.

    When you dump a NSDate with NSLog, you will get the date in its raw UTC form. To print the date using a local format, look into the NSDateFormatter class, which has a timeZone property:

    ....
    NSDate *date = [cal dateFromComponents:comp];
    NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
    fmt.timeZone = [NSTimeZone defaultTimeZone]; // Probably not required
    NSLog(@"date:%@", [fmt stringFromDate:date]);
    

提交回复
热议问题