gpt4 book ai didi

ios - 为什么 [日历 dateFromComponents :] always return hour 16 ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:44 26 4
gpt4 key购买 nike

当我将 [NSDate date] 传递给 NSDateComponents ,然后在修改工作日后传回时,我总是得到时间 16:00:00 。为什么?

代码如下:

     {
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *now = [NSDate date];
NSLog(@"now = %@",now);

NSDateComponents *firstDayOfWeek = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:now];
firstDayOfWeek.weekday = 1; // Monday as first day of week

NSDate *firstDayOfWeekDate = [calendar dateFromComponents:firstDayOfWeek];
NSLog(@"first day = %@", firstDayOfWeekDate);
}

{
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *now = [NSDate date];
NSLog(@"now = %@",now);

NSDateComponents *firstDayOfWeek = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:now];
firstDayOfWeek.weekday = 1; // Monday as first day of week
firstDayOfWeek.hour = 1;
firstDayOfWeek.minute = 1;
firstDayOfWeek.second = 1;

NSDate *firstDayOfWeekDate = [calendar dateFromComponents:firstDayOfWeek];
NSLog(@"first day = %@", firstDayOfWeekDate);
}

输出是:

 now = 2016-01-18 03:14:08 +0000
first day = 2016-01-17 16:00:00 +0000
now = 2016-01-18 03:14:08 +0000
first day = 2016-01-17 17:01:01 +0000

为什么小时从 16 开始?

最佳答案

NSDateComponents 没有到达 16:00:00。现在是您当前时区的午夜。但是 NSLog 显示的是等效的 GMT 时间。这就是 +0000 的意思:例如格林威治标准时间 +00:00。

最重要的是,如果您想在本地时区查看生成的 NSDate 对象,您应该使用 NSDateFormatter

关于ios - 为什么 [日历 dateFromComponents :] always return hour 16 ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34846785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com