【问题标题】:Get first day of the month [duplicate]获取当月的第一天[重复]
【发布时间】:2013-10-29 01:29:24
【问题描述】:

我正在尝试从当月的第一天获取 NSDate 对象。我使用以下代码:

    NSDateComponents *components = [c components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
    components.day = 1;

    NSDate *dayOneInCurrentMonth = [c dateFromComponents:components];

但是当我记录这个日期时,它给了我前一天

day one = 2013-09-30 22:00:00 +0000 

【问题讨论】:

  • 您的时区是 UTC -2 吗?在这种情况下,dayOneInCurrentMonth 是正确的日期,但是 NSLog 输出它并带有这个时区校正
  • @Jeepston 你和乔尔是对的。这是时区。

标签: ios objective-c nsdate nsdatecomponents


【解决方案1】:

您正在以 GMT 时区 (+0000) 记录日期。尝试记录:

NSLog(@"新日期:%@", [dayOneInCurrentMonth descriptionWithLocale:[NSLocale systemLocale]]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 2014-03-22
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多