<pre>
/**
- 获取当前的年月天;
*/
- (NSInteger)currentYearMonthDay:(NSDate *)currentDate{
NSCalendar calder = [NSCalendar currentCalendar];
NSDateComponents complement = [calder components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay | NSCalendarUnitHour fromDate:currentDate];
return complement.day;
}
/
- 获取每一个月的总天数
*/
- (NSInteger)totalDaysOfCurrentMounth:(NSDate *)currentDate{
NSRange range = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];
return range.length;
}
/**
*/
- (NSString *)getMonthBeginAndEndWith:(NSDate *)date{
// NSDateFormatter *format=[[NSDateFormatter alloc] init];
// [format setDateFormat:@"yyyy-MM"];
// NSDate *newDate=[format dateFromString:dateStr];
double interval = 0;
NSDate *beginDate = nil;
NSDate *endDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2];//设定周日为周首
BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:date];
//分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
if (ok) {
endDate = [beginDate dateByAddingTimeInterval:interval-1];
}else {
return @"";
}
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"YYYY.MM.dd"];
NSString *beginString = [myDateFormatter stringFromDate:beginDate];
NSString *endString = [myDateFormatter stringFromDate:endDate];
NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];
return s;
}
/**
*获取当前时间是星期几
*/
- (NSInteger)getWeekOfMonthWith:(NSDate )date{
NSCalendar calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2];
return [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfMonth forDate:date];
}
/
- 时间字符串转化为date
*/
- (NSDate )getDateWithTimeString:(NSString )timeStr{
NSDateFormatter inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSCalendarIdentifierGregorian] ];
[inputFormatter setDateFormat:@"yyyyMMdd"];
NSDate inputDate = [inputFormatter dateFromString:timeStr];
return inputDate;
}
/
每个月多少天
*/
+(int)getDaysWithYear:(NSInteger )year andMonth:(NSInteger)month{
int times [] = {31,28,31,30,31,30,31,31,30,31,30,31};
if ((year%4==0&&year%100!=0)||year%400==0) {
times[1] = 29;
}
return (times[month-1]);
} - (NSInteger)weeksOfMonthWithDate:(NSDate *)date{
NSInteger firstDay = [self firstWeekdayInThisMonth:date];
NSInteger totalDay = [self totalDaysOfCurrentMounth:date];
NSInteger otherWeeks = (totalDay - (7-firstDay))/7;
NSInteger otherDay = (totalDay - (7-firstDay))%7 == 0? 0:1 ;
return 1+otherWeeks +otherDay;
}
/**
- 第一天是周几
*/
- (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
[comp setDay:1];
NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
return firstWeekday - 1;
}
//获取某某年某某月的第一天是星期几
+(int)GetTheWeekOfDayByYera:(int)year
andByMonth:(int)month{
int sum = 0;
for(int i = 1;i<month;i++){
sum+=[self getDaysWithYear:year andMonth:i];
}
int nedDay = sum+1;
return ((year-1)+(year-1)/4 -(year/100)+(year/400)+nedDay)%7;
}
/**
-
获取某一个月的第一天
/
+(NSDate)startDayOfCurrentMonth:(NSDate *)date{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *startComplement = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth fromDate:date];
[startComplement setHour:+8];NSDate *startDate = [calendar dateFromComponents:startComplement];
return startDate;
}
(NSString *)autoFullString:(NSDate *)currentDate{
NSCalendar *calder = [NSCalendar currentCalendar];
NSDateComponents *complement = [calder components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay | NSCalendarUnitHour fromDate:currentDate];
if (complement.month<10) {
return [NSString stringWithFormat:@"%ld-0%ld",(long)complement.year,(long)complement.month];
}
return [NSString stringWithFormat:@"%ld-%ld",(long)complement.year,(long)complement.month];
}-
(NSDate *)lastMonth:(NSDate *)date{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.month = -1;NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
return newDate;
} (NSDate*)nextMonth:(NSDate *)date{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.month = +1;
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
return newDate;
}
</pre>
[转载一点其他]:http://www.111cn.net/sj/iOS/111617.htm
1、得到当前月第一天、最后一天日期
a、 本月开始日期
<pre>
func startOfCurrentMonth() -> NSDate {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year, .Month], fromDate: date)
let startOfMonth = calendar.dateFromComponents(components)!
return startOfMonth
}
</pre>
b、本月结束日期
<pre>
func endOfCurrentMonth(returnEndTime:Bool = false) -> NSDate {
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.month = 1
if returnEndTime {
components.second = -1
} else {
components.day = -1
}
let endOfMonth = calendar.dateByAddingComponents(components,
toDate: startOfCurrentMonth(),
options: [])!
return endOfMonth
}
</pre>
测试代码:
创建一个日期格式器
<pre>
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
</pre>
当月第一天日期
<pre>
let startDate = startOfCurrentMonth()
print("本月开始时间:", dateFormatter.stringFromDate(startDate))
</pre>
当月最后一天日期1<pre>
let endDate1 = endOfCurrentMonth()
print("本月结束时间1:", dateFormatter.stringFromDate(endDate1))
</pre>
当月最后一天日期2<pre>
let endDate2 = endOfCurrentMonth(true)
print("本月结束时间2:", dateFormatter.stringFromDate(endDate2))
</pre>
2、得到当年第一天、最后一天日期
a、本年开始日期
<pre>
func startOfCurrentYear() -> NSDate {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year], fromDate: date)
let startOfYear = calendar.dateFromComponents(components)!
return startOfYear
}
</pre>
b、本年结束日期
<pre>
func endOfCurrentYear(returnEndTime:Bool = false) -> NSDate {
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.year = 1
if returnEndTime {
components.second = -1
} else {
components.day = -1
}
let endOfYear = calendar.dateByAddingComponents(components,
toDate: startOfCurrentYear(),
options: [])!
return endOfYear
}
</pre>
测试代码:
创建一个日期格式器<pre>
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
</pre>
本年度第一天日期<pre>
let startDate = startOfCurrentYear()
print("本年度开始时间:", dateFormatter.stringFromDate(startDate))
</pre>
本年度最后一天日期1<pre>
let endDate1 = endOfCurrentYear()
print("本年度结束时间1:", dateFormatter.stringFromDate(endDate1))
</pre>
本年度最后一天日期2<pre>
let endDate2 = endOfCurrentYear(true)
print("本年度结束时间2:", dateFormatter.stringFromDate(endDate2))
</pre>
3、得到指定年月的第一天、最后一天日期
a、指定年月的开始日期<pre>
func startOfMonth(year: Int, month: Int) -> NSDate {
let calendar = NSCalendar.currentCalendar()
let startComps = NSDateComponents()
startComps.day = 1
startComps.month = month
startComps.year = year
let startDate = calendar.dateFromComponents(startComps)!
return startDate
}
</pre>
b、指定年月的结束日期 <pre>
func endOfMonth(year: Int, month: Int, returnEndTime:Bool = false) -> NSDate {
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.month = 1
if returnEndTime {
components.second = -1
} else {
components.day = -1
}
let endOfYear = calendar.dateByAddingComponents(components,
toDate: startOfMonth(year,
month:month), options: [])!
return endOfYear
} </pre>
测试代码:
创建一个日期格式器 <pre>
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
</pre>
当月第一天日期<pre>
let startDate = startOfMonth(2016, month: 8)
print("2016年8月的开始时间:", dateFormatter.stringFromDate(startDate))
</pre>
当月最后一天日期1<pre>
let endDate1 = endOfMonth(2016, month: 8)
print("2016年8月的结束时间1:", dateFormatter.stringFromDate(endDate1))
</pre>
当月最后一天日期2<pre>
let endDate2 = endOfMonth(2016, month: 8, returnEndTime: true)
print("2016年8月的结束时间2:", dateFormatter.stringFromDate(endDate2))
</pre>