ios - 为什么 [日历 dateFromComponents :] always return hour 16 ?
<p><p>当我将 传递给 NSDateComponents ,然后在修改工作日后传回时,我总是得到小时 16:00:00 。为什么?</p>
<p>代码如下:</p>
<pre><code> {
NSCalendar *calendar = [initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *now = ;
NSLog(@"now = %@",now);
NSDateComponents *firstDayOfWeek = ;
firstDayOfWeek.weekday = 1; // Monday as first day of week
NSDate *firstDayOfWeekDate = ;
NSLog(@"first day = %@", firstDayOfWeekDate);
}
{
NSCalendar *calendar = [initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *now = ;
NSLog(@"now = %@",now);
NSDateComponents *firstDayOfWeek = ;
firstDayOfWeek.weekday = 1; // Monday as first day of week
firstDayOfWeek.hour = 1;
firstDayOfWeek.minute = 1;
firstDayOfWeek.second = 1;
NSDate *firstDayOfWeekDate = ;
NSLog(@"first day = %@", firstDayOfWeekDate);
}
</code></pre>
<p>输出是:</p>
<pre><code> 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
</code></pre>
<p>为什么小时以 16 开头? </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><code>NSDateComponents</code> 不是 <code>16:00:00</code>。它正在您当前时区的午夜。但是 <code>NSLog</code> 显示的是等效的 GMT 时间。这就是 <code>+0000</code> 的含义:例如格林威治标准时间 +00:00。</p>
<p>底线,如果你想在你的本地时区看到生成的 <code>NSDate</code> 对象,你应该使用 <code>NSDateFormatter</code>。</p></p>
<p style="font-size: 20px;">关于ios - 为什么 [日历 dateFromComponents :] always return hour 16 ?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34846785/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34846785/
</a>
</p>
页:
[1]