NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];[components setDay:1];self.currentDate = [calendar dateFromComponents:components];int m = components.month;int y = components.year;int d = components.day;log: 2012,5,1
我怎么可以得到lastdate od个月?
请给我一些建议,谢谢!
解决方法 我写的一些NSDate类别方法可以帮助你:这是一个简单的方法来找到一个月的开始:
(NSDate *) startOfMonth{ NSCalendar * calendar = [NSCalendar currentCalendar]; NSDateComponents * currentDateComponents = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit fromDate: self]; NSDate * startOfMonth = [calendar dateFromComponents: currentDateComponents]; return startOfMonth;}
所有这一切都是从当天开始的年份和月份组件,然后再次转换为日期.
在本月底,我使用了几种方法:
- (NSDate *) dateByAddingMonths: (NSInteger) monthsToAdd{ NSCalendar * calendar = [NSCalendar currentCalendar]; NSDateComponents * months = [[NSDateComponents alloc] init]; [months setMonth: monthsToAdd]; return [calendar dateByAddingComponents: months toDate: self options: 0];}
和
- (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;}
这是一个3步的过程 – 在当前日期添加一个月,找到下个月的开始,然后减去1秒,以找到本月底.
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,from: plusOneMonthDate) let endOfMonth = calendar.date(from: plusOneMonthDateComponents)?.addingTimeInterval(-1) return endOfMonth }}
斯威夫特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 }}总结
以上是内存溢出为你收集整理的ios – 获得firstdate,月份的最后日期全部内容,希望文章能够帮你解决ios – 获得firstdate,月份的最后日期所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)