我想获得第一个月,最后一个月,我尝试
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
我怎样才能获得最后一个月?
请给我一些建议,谢谢!!答案 0 :(得分:28)
我写的一些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, .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
}
}
答案 1 :(得分:7)
尝试这个技巧。
Day Param:1是获得本月的第一个日期
Day Param:0是通过获取下个月之前的月份的最后日期来获取本月的最后日期(意味着本月))
- (void)returnDate:(NSDate *)date {
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
NSDate * firstDateOfMonth = [self returnDateForMonth:comps.month year:comps.year day:1];
NSDate * lastDateOfMonth = [self returnDateForMonth:comps.month+1 year:comps.year day:0];
NSLog(@"date %@", date); // date 2013-06-20
NSLog(@"First %@", firstDateOfMonth); // firstDateOfMonth 2013-06-01
NSLog(@"Last %@", lastDateOfMonth); // lastDateOfMonth 2013-06-30
}
下一步
- (NSDate *)returnDateForMonth:(NSInteger)month year:(NSInteger)year day:(NSInteger)day {
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:day];
[components setMonth:month];
[components setYear:year];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
return [gregorian dateFromComponents:components];
}
不要忘记正确的日期形式
针对IOS 8.0进行了更新
- (void)returnDate:(NSDate *)date {
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth;
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
NSDate * firstDateOfMonth = [self returnDateForMonth:comps.month year:comps.year day:1];
NSDate * lastDateOfMonth = [self returnDateForMonth:comps.month+1 year:comps.year day:0];
NSLog(@"date %@", date); // date 2013-06-20
NSLog(@"First %@", firstDateOfMonth); // firstDateOfMonth 2013-06-01
NSLog(@"Last %@", lastDateOfMonth); // lastDateOfMonth 2013-06-30
}
下一步
- (NSDate *)returnDateForMonth:(NSInteger)month year:(NSInteger)year day:(NSInteger)day {
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:day];
[components setMonth:month];
[components setYear:year];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
return [gregorian dateFromComponents:components];
}
现在你正在使用:
答案 2 :(得分:3)
好的,因为我正在滚动我自己的日历控件,所以我继续创建了一个NSCalendar
类别。这是我的lastOfMonth
方法:
- (NSDate *)lastOfMonth:(NSDate *)date
{
NSDateComponents *dc = [self components:NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit fromDate:date];
NSRange dim = [self rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
dc.day = dim.length;
return [self dateFromComponents:dc];
}
答案 3 :(得分:2)
Swift 2.0 获取当月的第一天:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components([NSCalendarUnit.Month, NSCalendarUnit.Year], fromDate: date)
let firstDay = self.returnDateForMonth(dateComponents.month, year: dateComponents.year, day: 1)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MMM-yy"
print("First day of this month: \(firstDay)") // 01-Sep-15
执行此操作的功能:
func returnDateForMonth(month:NSInteger, year:NSInteger, day:NSInteger)->NSDate{
let comp = NSDateComponents()
comp.month = month
comp.year = year
comp.day = day
let grego = NSCalendar.currentCalendar()
return grego.dateFromComponents(comp)!
}
答案 4 :(得分:1)
您可以使用以下方式获取月份的最后日期: -
NSDate currDate=[NSDate date];
-(NSString *)getLastDateMonth:(NSDate *)currDate{
NSCalendar *gregCalendar=[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components=[gregCalendar components:NSMonthCalendarUnit|NSYearCalendarUnit fromDate:currDate];
NSInteger month=[components month];
NSInteger year=[components year];
if (month==12) {
[components setYear:year+1];
[components setMonth:1];
} else {
[components setMonth:month+1];
}
[components setDay:1];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *lastDateOfMonth = [dateFormat stringFromDate:(NSDate *)[[gregCalendar dateFromComponents:components] dateByAddingTimeInterval:-86400]];
dateFormat=nil;
return lastDateOfMonth;
}
答案 5 :(得分:1)
在基于纳齐尔的斯威夫特的回答
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="https://your_https_url" exactDestination="true" childOnly="false" />
</system.webServer>
</configuration>
答案 6 :(得分:0)
看起来不需要任何技巧或黑客攻击, NSCalendar rangeOfUnit 可以很好地完成工作:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *dc = [[NSDateComponents alloc] init];
dc.year = 1947;
dc.month = 8; //desired month
//calculate start date
dc.day = 1; //As far as I can think start date of a month will always be 1
NSDate *start = [cal dateFromComponents:dc];
//calculate last date of month
NSRange dim = [cal rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:start]; // we used start date as it has the same month & year value as desired
dc.day=dim.length; //this is the simply the last day of the desired month
NSDate *last = [[NSCalendar currentCalendar] dateFromComponents:dc];
记录验证值:
NSLog(@"Start day of month Date: %@", start);
NSLog(@"Last day of month Date: %@", last);
Apple文档关于NSCalendar rangeOfUnit:https://developer.apple.com/documentation/foundation/nscalendar/1418344-rangeofunit
p.s看起来像@Scott Means的答案使用相同的逻辑,但不需要任何类别,所以请使用你喜欢的答案!
答案 7 :(得分:0)
迅速5月的最后一天-基于不同的来源
// -------------------------------------------------------
// lastDayOfMonth
// -------------------------------------------------------
static func lastDayOfMonth(year:Int, month:Int) -> Int {
let calendar:Calendar = Calendar.current
var dc:DateComponents = DateComponents()
dc.year = year
dc.month = month + 1
dc.day = 0
let lastDateOfMonth:Date = calendar.date(from:dc)!
let lastDay = calendar.component(Calendar.Component.day, from: lastDateOfMonth)
return lastDay
}