【问题标题】:NSDate hours are not correct after constructing date from components从组件构建日期后,NSDate 小时不正确
【发布时间】:2012-06-03 22:53:55
【问题描述】:

我正在尝试从两个不同的日期构建一个日期。当我将两个日期分解成各自的组成部分并将它们重新组合成新的日期时……除了时间之外,一切似乎都是正确的。我确定它与时区或 NSGregorianCalendar 有一些简单的关系,但我是 iOS 新手,并且对 NSCalendar 进行了编程。我希望有人能抓住我犯的简单错误。

//grab today's date so we can brek it down into components
NSDate *todayDate = [NSDate date];

NSLog(@"Todays Date: %@", todayDate);

NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

//break down the stored date into components
NSDateComponents *theTime = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:storedIncorrectDate];

NSLog(@"Incorrect Calendar Components %@", theTime);

NSDateComponents *todayCalendarComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:todayDate];

NSInteger currentYear = [todayCalendarComponents year];

NSLog(@"The Year: %i", currentYear);

NSInteger currentMonth = [todayCalendarComponents month];

NSLog(@"The Month: %i", currentMonth);

NSInteger currentDay = [todayCalendarComponents day];

NSLog(@"The Day: %i", currentDay);


NSLog(@"Today's Calendar Components %@", todayCalendarComponents);


NSInteger theHours = [theTime hour];

NSLog(@"Hours: %i", theHours);

NSInteger theMinutes = [theTime minute];

NSLog(@"Minutes: %i", theMinutes);


NSInteger theSeconds = [theTime second];

NSLog(@"Seconds: %i", theSeconds);

//build my new date and store it before we play the affirmation.
NSDateComponents *components = [[NSDateComponents alloc] init];


[components setHour:theHours];
[components setMinute:theMinutes]; 
[components setSecond:theSeconds]; 
[components setYear:currentYear];
[components setMonth:currentMonth];
[components setDay:currentDay];
[components setTimeZone:[NSTimeZone localTimeZone]];


NSLog(@"The Components: %@", components);

NSCalendar *updatedCal = [[NSCalendar alloc]
                          initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *revisedCorrectedDate = [updatedCal dateFromComponents:components]; 

NSLog(@"The Hours: %i", theHours);

NSLog(@"The New and Corrected Date: %@", revisedCorrectedDate);

打印revisedCorrectedDate 的日志结果显示我设置的所有内容都是正确的,但小时数除外。这必须是一个简单的修复..我只是没有看到它。提前谢谢!

【问题讨论】:

    标签: xcode ios5 nsdate nscalendar


    【解决方案1】:

    你确定它不正确吗?当我运行这段代码时,我得到:

    2012-06-03 18:13:36.130 无标题[39805:707] 新的和更正的 日期:2012-06-04 01:13:36 +0000

    这里现在是 6/3/12 18:13:36,显示的日期是 6/4/12 01:13:36,这是正确的时间,以 UTC 表示(注意末尾的 +0000) .

    【讨论】:

    • zpasterack ......你是对的。现在正在调查一些事情,一旦我深入研究就会做出回应。到目前为止非常感谢。敬请期待!
    • zpastermack,如果您有任何快速的想法可以为我节省一些研究,这就是我想要做的。我只需要将 UTC 转换为本地时区,以标准日期格式将其存储在数据库中。
    • 想通了.. 是我第一次使用组件来改变 NSDate。意识到当它将我的 UTC 时间存储在数据库中时,它会将其转换为我的本地时间。
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多