I want to get firstdate、lastdate of month,I try
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
[components setDay:1];
self.c
Some NSDate
category methods I wrote will help you:
Here's an easy way to find the start of a month:
(NSDate *) startOfMonth
{
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * currentDateComponents = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit fromDate: self];
NSDate * startOfMonth = [calendar dateFromComponents: currentDateComponents];
return startOfMonth;
}
All it does is take the year and month components from the current day, then convert back to a date again.
For the end of the month, I use a couple of methods:
- (NSDate *) dateByAddingMonths: (NSInteger) monthsToAdd
{
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * months = [[NSDateComponents alloc] init];
[months setMonth: monthsToAdd];
return [calendar dateByAddingComponents: months toDate: self options: 0];
}
and
- (NSDate *) endOfMonth
{
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * plusOneMonthDate = [self dateByAddingMonths: 1];
NSDateComponents * plusOneMonthDateComponents = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit fromDate: plusOneMonthDate];
NSDate * endOfMonth = [[calendar dateFromComponents: plusOneMonthDateComponents] dateByAddingTimeInterval: -1]; // One second before the start of next month
return endOfMonth;
}
This is a 3 step process - add one month to the current date, find the start of the next month, then subtract 1 second to find the end of this month.
Swift 3
extension Date {
func startOfMonth() -> Date? {
let calendar = Calendar.current
let currentDateComponents = calendar.dateComponents([.year, .month], from: self)
let startOfMonth = calendar.date(from: currentDateComponents)
return startOfMonth
}
func dateByAddingMonths(_ monthsToAdd: Int) -> Date? {
let calendar = Calendar.current
var months = DateComponents()
months.month = monthsToAdd
return calendar.date(byAdding: months, to: self)
}
func endOfMonth() -> Date? {
guard let plusOneMonthDate = dateByAddingMonths(1) else { return nil }
let calendar = Calendar.current
let plusOneMonthDateComponents = calendar.dateComponents([.year, .month], from: plusOneMonthDate)
let endOfMonth = calendar.date(from: plusOneMonthDateComponents)?.addingTimeInterval(-1)
return endOfMonth
}
}
Swift 2
extension NSDate {
func startOfMonth() -> NSDate? {
let calendar = NSCalendar.currentCalendar()
let currentDateComponents = calendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: self)
let startOfMonth = calendar.dateFromComponents(currentDateComponents)
return startOfMonth
}
func dateByAddingMonths(monthsToAdd: Int) -> NSDate? {
let calendar = NSCalendar.currentCalendar()
let months = NSDateComponents()
months.month = monthsToAdd
return calendar.dateByAddingComponents(months, toDate: self, options: nil)
}
func endOfMonth() -> NSDate? {
let calendar = NSCalendar.currentCalendar()
if let plusOneMonthDate = dateByAddingMonths(1) {
let plusOneMonthDateComponents = calendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: plusOneMonthDate)
let endOfMonth = calendar.dateFromComponents(plusOneMonthDateComponents)?.dateByAddingTimeInterval(-1)
return endOfMonth
}
return nil
}
}