问题描述
我想获取月份的第一日期,最后日期,我尝试
I want to get firstdate、lastdate of month,I try
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
我如何获得月份的最后日期?
How can i get lastdate od month?
请给我一些建议,谢谢!
please give me some advice,thank you!!
推荐答案
我编写的某些NSDate
类别方法将为您提供帮助:
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];
}
和
- (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;
}
这是一个三步过程-在当前日期前添加一个月,找到下个月的开始,然后减去1秒来查找本月末.
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
}
}
快捷键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
}
}
这篇关于获取月份的第一日期,最后一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!