NSDate 表示该月的第一天

发布于 2024-09-19 20:15:16 字数 291 浏览 19 评论 0原文

这是一个非常简单的概念,但到目前为止我还无法找到一个优雅的(与日历区域设置无关的)解决方案。我需要找到任意 NSDate 的该月的第一天。例如,给定一个任意 NSDate (任意日期),将返回另一个 NSDate 对象(我们称之为 firstDayOfMonthDate),它表示该日期的第一天任意日期中的月份。时间部分并不重要,因为我只需要该月第一天的 NSDate 对象(尽管为了整洁起见,如果时间被清零的话会很有用)。

预先感谢您的任何帮助。

This is quite a simple concept, but as of yet I have been unable to find an elegant (and calendar locale independent) solution. I need to find the first day of the month for an arbitrary NSDate. For example, given an arbitrary NSDate (arbitraryDate) another NSDate object will be returned (let's call this firstDayOfMonthDate) which represents the first day of the month in arbitraryDate. The time component does not matter, as I just need the NSDate object for the first day of the month (although for neatness it would be useful if the time was just zeroed out).

Thanks in advance for any assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何其悲哀 2024-09-26 20:15:16

一个可能的解决方案:

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

A possible solution:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *arbitraryDate = [NSDate date];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:arbitraryDate];
[comp setDay:1];
NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];
尬尬 2024-09-26 20:15:16
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear 
                                                               fromDate:[NSDate date]];
components.day = 1;
NSDate *firstDayOfMonthDate = [[NSCalendar currentCalendar] dateFromComponents: components];
NSLog(@"First day of month: %@", [firstDayOfMonthDate descriptionWithLocale:[NSLocale currentLocale]]);
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear 
                                                               fromDate:[NSDate date]];
components.day = 1;
NSDate *firstDayOfMonthDate = [[NSCalendar currentCalendar] dateFromComponents: components];
NSLog(@"First day of month: %@", [firstDayOfMonthDate descriptionWithLocale:[NSLocale currentLocale]]);
溺ぐ爱和你が 2024-09-26 20:15:16

Swift 3

我已经包含了获取该月的最后一天的信息,以防它对任何人有用。

extension Date {
    func lastDayOfMonth() -> Date {
        let calendar = Calendar.current
        let dayRange = calendar.range(of: .day, in: .month, for: self)
        let dayCount = dayRange!.count
        var comp = calendar.dateComponents([.year, .month, .day], from: self)

        comp.day = dayCount

        return calendar.date(from: comp)!
    }

    func firstDayOfMonth() -> Date {
        let calendar: Calendar = Calendar.current
        var components: DateComponents = calendar.dateComponents([.year, .month, .day], from: self)
        components.setValue(1, for: .day)
        return calendar.date(from: components)!
    }
}

Swift 3

I've included getting the last day of the month in case it is useful to anyone.

extension Date {
    func lastDayOfMonth() -> Date {
        let calendar = Calendar.current
        let dayRange = calendar.range(of: .day, in: .month, for: self)
        let dayCount = dayRange!.count
        var comp = calendar.dateComponents([.year, .month, .day], from: self)

        comp.day = dayCount

        return calendar.date(from: comp)!
    }

    func firstDayOfMonth() -> Date {
        let calendar: Calendar = Calendar.current
        var components: DateComponents = calendar.dateComponents([.year, .month, .day], from: self)
        components.setValue(1, for: .day)
        return calendar.date(from: components)!
    }
}
苏大泽ㄣ 2024-09-26 20:15:16

迅捷版

let arbitraryDate:NSDate = NSDate()
let calendar:NSCalendar = NSCalendar.currentCalendar()
let components:NSDateComponents = calendar.components([.Year, .Month, .Day], fromDate: arbitraryDate)
components.setValue(1, forComponent: .Day)
let firstDayOfMonthDate = calendar.dateFromComponents(components)

Swift version

let arbitraryDate:NSDate = NSDate()
let calendar:NSCalendar = NSCalendar.currentCalendar()
let components:NSDateComponents = calendar.components([.Year, .Month, .Day], fromDate: arbitraryDate)
components.setValue(1, forComponent: .Day)
let firstDayOfMonthDate = calendar.dateFromComponents(components)
朕就是辣么酷 2024-09-26 20:15:16

斯威夫特3:

let calendar = Calendar.current
var comps = calendar.dateComponents([.year, .month], from: date)
comps.setValue(1, for: .day)
let firstDayOfMonth = calendar.date(from: comps)!

// I tried this, but it doesn't work, why?
// let firstDayOfMonth = calendar.date(bySetting: .day, value: 1, of: date)!

Swift 3:

let calendar = Calendar.current
var comps = calendar.dateComponents([.year, .month], from: date)
comps.setValue(1, for: .day)
let firstDayOfMonth = calendar.date(from: comps)!

// I tried this, but it doesn't work, why?
// let firstDayOfMonth = calendar.date(bySetting: .day, value: 1, of: date)!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文