gpt4 book ai didi

iphone - 使用 NSCalendar 检索每月的第一天

转载 作者:太空狗 更新时间:2023-10-30 03:49:12 26 4
gpt4 key购买 nike

我执行以下代码片段:

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[comp setDay:1];

NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];

firstDayOfMonthDate 的值是 = 30-4-2011 但期望值是 1-5-2011 即我想要任何指定日期的月份的第一个日期。但它给了我上个月的最后一个日期。

提前致谢。

最佳答案

添加

[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

在初始化你的 NSCalendar 之后。

[NSDate date] 将返回 UTC 时区的日期,以及自 2001 年 1 月 1 日 00:00 GMT 以来的绝对间隔,但 NSCalendar 将其时区设置为默认时区,这可能不是 UTC。

UTC 与 UTC“相同”,引用自维基百科:

In casual use, when fractions of a second are not important, Greenwich Mean Time (GMT) can be considered equivalent to UTC or UT1. Saying "GMT" often implies either UTC or UT1 when used within informal or casual contexts. In technical contexts, usage of "GMT" is avoided; the unambiguous terminology "UTC" or "UT1" is preferred.[3]

关于iphone - 使用 NSCalendar 检索每月的第一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5920608/

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