<pre>
/**
- 獲取當(dāng)前的年月天起宽;
*/
- (NSInteger)currentYearMonthDay:(NSDate *)currentDate{
NSCalendar calder = [NSCalendar currentCalendar];
NSDateComponents complement = [calder components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay | NSCalendarUnitHour fromDate:currentDate];
return complement.day;
}
/
- 獲取每一個(gè)月的總天數(shù)
*/
- (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];//設(shè)定周日為周首
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;
}
/**
*獲取當(dāng)前時(shí)間是星期幾
*/
- (NSInteger)getWeekOfMonthWith:(NSDate )date{
NSCalendar calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2];
return [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfMonth forDate:date];
}
/
- 時(shí)間字符串轉(zhuǎn)化為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;
}
/
每個(gè)月多少天
*/
+(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;
}
/**
-
獲取某一個(gè)月的第一天
/
+(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>
[轉(zhuǎn)載一點(diǎn)其他]:http://www.111cn.net/sj/iOS/111617.htm
1洲胖、得到當(dāng)前月第一天、最后一天日期
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绿映、本月結(jié)束日期
<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>
測試代碼:
創(chuàng)建一個(gè)日期格式器
<pre>
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
</pre>
當(dāng)月第一天日期
<pre>
let startDate = startOfCurrentMonth()
print("本月開始時(shí)間:", dateFormatter.stringFromDate(startDate))
</pre>
當(dāng)月最后一天日期1<pre>
let endDate1 = endOfCurrentMonth()
print("本月結(jié)束時(shí)間1:", dateFormatter.stringFromDate(endDate1))
</pre>
當(dāng)月最后一天日期2<pre>
let endDate2 = endOfCurrentMonth(true)
print("本月結(jié)束時(shí)間2:", dateFormatter.stringFromDate(endDate2))
</pre>
2、得到當(dāng)年第一天腐晾、最后一天日期
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、本年結(jié)束日期
<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>
測試代碼:
創(chuàng)建一個(gè)日期格式器<pre>
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
</pre>
本年度第一天日期<pre>
let startDate = startOfCurrentYear()
print("本年度開始時(shí)間:", dateFormatter.stringFromDate(startDate))
</pre>
本年度最后一天日期1<pre>
let endDate1 = endOfCurrentYear()
print("本年度結(jié)束時(shí)間1:", dateFormatter.stringFromDate(endDate1))
</pre>
本年度最后一天日期2<pre>
let endDate2 = endOfCurrentYear(true)
print("本年度結(jié)束時(shí)間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巨柒、指定年月的結(jié)束日期 <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>
測試代碼:
創(chuàng)建一個(gè)日期格式器 <pre>
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
</pre>
當(dāng)月第一天日期<pre>
let startDate = startOfMonth(2016, month: 8)
print("2016年8月的開始時(shí)間:", dateFormatter.stringFromDate(startDate))
</pre>
當(dāng)月最后一天日期1<pre>
let endDate1 = endOfMonth(2016, month: 8)
print("2016年8月的結(jié)束時(shí)間1:", dateFormatter.stringFromDate(endDate1))
</pre>
當(dāng)月最后一天日期2<pre>
let endDate2 = endOfMonth(2016, month: 8, returnEndTime: true)
print("2016年8月的結(jié)束時(shí)間2:", dateFormatter.stringFromDate(endDate2))
</pre>