/獲取年月日對(duì)象??
+(NSDateComponents?*)getDateComponents:(NSDate?*)date?{??
NSCalendar?*calendar?=?[NSCalendar?currentCalendar];??
//????[calendar?setFirstWeekday:2];?//設(shè)置每周的開始是星期一??
//????[calendar?setMinimumDaysInFirstWeek:7];?//設(shè)置一周至少需要幾天??
return?[calendar?components:??
????????????NSYearCalendarUnit|??
????????????NSMonthCalendarUnit|??
????????????NSDayCalendarUnit|??
????????????NSWeekOfYearCalendarUnit|??
NSQuarterCalendarUnit?fromDate:date];??
}??
//獲得某年的周數(shù)??
+(NSInteger)getWeek_AccordingToYear:(NSInteger)year?{??
NSDateComponents?*comps?=?[DataTimeTool?getDateComponents:[DataTimeTool?dateFromString:[NSString?stringWithFormat:@"%ld-12-31",year]?DateFormat:@"yyyy-MM-dd"]];??
NSInteger?week?=?[comps?weekOfYear];??
if?(week?==?1)?{??
return?52;??
}else?{??
return?week;??
????}??
}??
/**
?*??獲取某年某周的范圍日期
?*
?*??@param?year???????年份
?*??@param?weekofYear?year里某個(gè)周
?*
?*??@return?時(shí)間范圍字符串
?*/??
+(NSString*)getWeekRangeDate_Year:(NSInteger)year?WeakOfYear:(NSInteger)weekofYear??
{??
NSString?*weekDate?=?@"";??
NSString?*timeAxis?=?[NSString?stringWithFormat:@"%ld-06-01?12:00:00",(long)year];??
NSDateFormatter?*dateFormatter?=?[[NSDateFormatter?alloc]?init];??
[dateFormatter?setDateFormat:@"yyyy-MM-dd?HH:mm:ss"];??
NSDate?*date?=?[dateFormatter?dateFromString:timeAxis];??
NSCalendar?*calendar?=?[NSCalendar?currentCalendar];??
/**這兩個(gè)參數(shù)的設(shè)置影響著周次的個(gè)數(shù)和劃分*****************/??
[calendar?setFirstWeekday:2];?//設(shè)置每周的開始是星期一??
//????[calendar?setMinimumDaysInFirstWeek:7];?//設(shè)置一周至少需要幾天??
NSDateComponents?*comps?=?[calendar?components:(NSCalendarUnitWeekOfYear?|?NSCalendarUnitWeekday?|?NSCalendarUnitWeekdayOrdinal?|?NSCalendarUnitYear?|?NSCalendarUnitMonth?|?NSCalendarUnitDay)??
?fromDate:date];??
//時(shí)間軸是當(dāng)前年的第幾周??
NSInteger?todayIsWeek?=?[comps?weekOfYear];??
if?([DataTimeTool?getWeek_AccordingToYear:year]?==?53)?{??
todayIsWeek?+=1;??
????}??
//獲取時(shí)間軸是星期幾?1(星期天)?2(星期一)?3(星期二)?4(星期三)?5(星期四)?6(星期五)?7(星期六)??
NSInteger?todayIsWeekDay?=?[comps?weekday];??
//?計(jì)算當(dāng)前日期和這周的星期一和星期天差的天數(shù)??
//firstDiff?星期一相差天數(shù)?魂毁、?lastDiff?星期天相差天數(shù)??
long?firstDiff,lastDiff;??
if?(todayIsWeekDay?==?1)?{??
firstDiff?=?-6;??
lastDiff?=0;??
}else??
????{??
firstDiff?=?[calendar?firstWeekday]?-?todayIsWeekDay;??
lastDiff?=8?-?todayIsWeekDay;??
????}??
NSDate?*firstDayOfWeek=?[NSDate?dateWithTimeInterval:24*60*60*firstDiff?sinceDate:date];??
NSDate?*lastDayOfWeek=?[NSDate?dateWithTimeInterval:24*60*60*lastDiff?sinceDate:date];??
long?weekdifference?=?weekofYear?-?todayIsWeek;??
firstDayOfWeek=?[NSDate?dateWithTimeInterval:24*60*60*7*weekdifference?sinceDate:firstDayOfWeek];??
lastDayOfWeek=?[NSDate?dateWithTimeInterval:24*60*60*7*weekdifference?sinceDate:lastDayOfWeek];??
weekDate?=?[NSString?stringWithFormat:@"第%ld周(%@-%@)",weekofYear,[DataTimeTool?stringFromDate:firstDayOfWeek?DateFormat:@"yyyy年M月d號(hào)"],[DataTimeTool?stringFromDate:lastDayOfWeek?DateFormat:@"yyyy年M月d號(hào)"]];??
return?weekDate;??
}??
/**************************當(dāng)前時(shí)間********************************/??
+(NSDateComponents?*)getCurrentDateComponents?{??
return?[DataTimeTool?getDateComponents:[NSDate?date]];??
}??
+(NSInteger)getCurrentWeek?{??
NSInteger?week?=?[[DataTimeTool?stringFromDate:[NSDate?date]?DateFormat:@"w"]?intValue];??
return?week;??
}??
+(NSInteger)getCurrentYear{??
NSInteger?year?=?[[DataTimeTool?stringFromDate:[NSDate?date]?DateFormat:@"y"]?intValue];??
return?year;??
}??
+(NSInteger)getCurrentQuarter{??
NSInteger?quarter?=?[[DataTimeTool?stringFromDate:[NSDate?date]?DateFormat:@"q"]?intValue];??
return?quarter;??
}??
+(NSInteger)getCurrentMonth{??
NSInteger?month?=?[[DataTimeTool?stringFromDate:[NSDate?date]?DateFormat:@"M"]?intValue];??
return?month;??
}??
+(NSInteger)getCurrentDay{??
NSInteger?day?=?[[DataTimeTool?stringFromDate:[NSDate?date]?DateFormat:@"d"]?intValue];??
return?day;??
}??
//NSString轉(zhuǎn)NSDate??
+(NSDate?*)dateFromString:(NSString?*)dateString?DateFormat:(NSString?*)DateFormat{??
NSDateFormatter?*dateFormatter?=?[[NSDateFormatter?alloc]?init];??
[dateFormatter?setDateFormat:DateFormat];??
NSDate?*destDate=?[dateFormatter?dateFromString:dateString];??
return?destDate;??
}??
//NSDate轉(zhuǎn)NSString??
+(NSString?*)stringFromDate:(NSDate?*)date?DateFormat:(NSString?*)DateFormat{??
NSDateFormatter?*dateFormatter?=?[[NSDateFormatter?alloc]?init];??
[dateFormatter?setDateFormat:DateFormat];??
NSString?*destDateString?=?[dateFormatter?stringFromDate:date];??
return?destDateString;??
}??
//時(shí)間追加??
+(NSString?*)dateByAddingTimeInterval:(NSTimeInterval)TimeInterval?DataTime:(NSString?*)dateStr?DateFormat:(NSString?*)DateFormat{??
NSString?*str?=?nil;??
NSDate?*date?=?[self?dateFromString:dateStr?DateFormat:DateFormat];??
NSDate?*?newDate?=?[date?dateByAddingTimeInterval:TimeInterval];??
str?=?[self?stringFromDate:newDate?DateFormat:DateFormat];??
return?str;??
}??
//日期字符串格式化??
+(NSString?*)getDataTime:(NSString?*)dateStr?DateFormat:(NSString?*)DateFormat?{??
return?[self?getDataTime:dateStr?DateFormat:DateFormat?oldDateFormat:nil];??
}??
+(NSString?*)getDataTime:(NSString?*)dateStr?DateFormat:(NSString?*)DateFormat?oldDateFormat:(NSString?*)oldDateFormat{??
if?(!dateStr?||?[dateStr?isEqualToString:@"—"])?{??
return?@"—";??
????}??
if?([dateStr?isEqualToString:@"0"])?{??
return?@"0";??
????}??
if?([dateStr?rangeOfString:@"+"].location?!=?NSNotFound)?{??
NSArray?*strarray?=?[dateStr?componentsSeparatedByString:@"+"];??
dateStr?=?strarray[0];??
????}??
if?([dateStr?rangeOfString:@"."].location?!=?NSNotFound)?{??
NSArray?*strarray?=?[dateStr?componentsSeparatedByString:@"."];??
dateStr?=?strarray[0];??
????}??
if?([dateStr?rangeOfString:@"T"].location?!=?NSNotFound)?{??
dateStr?=?[dateStr?stringByReplacingOccurrencesOfString:@"T"?withString:@"?"];??
????}??
NSDateFormatter?*newDateFormatter?=?[[NSDateFormatter?alloc]init];??
[newDateFormatter?setDateFormat:DateFormat];??
NSDateFormatter?*oldDateFormatter?=?[[NSDateFormatter?alloc]?init];??
if?(oldDateFormat)?{??
[oldDateFormatter?setDateFormat:oldDateFormat];??
}else?{??
[oldDateFormatter?setDateFormat:[self?getFormat:dateStr]];??
????}??
NSDate?*date?=?[oldDateFormatter?dateFromString:dateStr];??
return?[newDateFormatter?stringFromDate:date];??
}??
+(int)getNumberOfCharactersInString:(NSString?*)str?c:(char)c?{??
int?count?=?0;??
if?([str?rangeOfString:[NSString?stringWithFormat:@"%c",c]].location?!=?NSNotFound){??
for?(int?i=0;i
if?([str?characterAtIndex:i]?==?c){??
????????????????count++;??
????????????}??
????????}??
????}??
return?count;??
}??
+(NSString?*)getFormat:(NSString?*)dateString?{??
NSString?*str?=?[NSString?new];??
int?size?=?[self?getNumberOfCharactersInString:dateString?c:'-'];??
if?(size?==?0){??
str?=?[str?stringByAppendingString:@"yyyy"];??
}else?if?(size?==?1){??
str?=?[str?stringByAppendingString:@"yyyy-MM"];??
}else?if?(size?==?2){??
str?=?[str?stringByAppendingString:@"yyyy-MM-dd"];??
????}??
size?=?[self?getNumberOfCharactersInString:dateString?c:':'];??
if?(size?==?0?&&?[dateString?rangeOfString:@"?"].location?!=?NSNotFound){??
str?=?[str?stringByAppendingString:@"?HH"];??
}else?if?(size?==?1){??
str?=?[str?stringByAppendingString:@"?HH:mm"];??
}else?if?(size?==?2){??
str?=?[str?stringByAppendingString:@"?HH:mm:ss"];??
????}??
return?str;??
}??
/**
?*??json日期轉(zhuǎn)iOS時(shí)間
?*
?*??@param?string?/Date()
?*
?*??@return
?*/??
+(NSString?*)interceptTimeStampFromStr:(NSString?*)string?DateFormat:(NSString?*)DateFormat{??
if?(!string?||?[string?length]?==?0?)?//?傳入時(shí)間戳為空?返回??
????{??
return?@"—";??
????}??
NSMutableString?*?mutableStr?=?[NSMutableString?stringWithString:string];??
NSString?*?timeStampString?=?[NSString?string];??
//??遍歷取出括號(hào)內(nèi)的時(shí)間戳??
for?(int?i?=?0;?i?<?string.length;?i?++)?{??
NSRange?startRang?=?[mutableStr?rangeOfString:@"("];??
NSRange?endRang?=?[mutableStr?rangeOfString:@")"];??
if?(startRang.location?!=?NSNotFound)?{??
//?左邊括號(hào)位置??
NSInteger?leftLocation?=?startRang.location;??
//?右邊括號(hào)距離左邊括號(hào)的長(zhǎng)度??
NSInteger?rightLocation?=?endRang.location?-?startRang.location;??
//?截取括號(hào)時(shí)間戳內(nèi)容??
timeStampString?=?[mutableStr?substringWithRange:NSMakeRange(leftLocation?+?1,rightLocation?-?1)];??
????????}??
????}??
//?把時(shí)間戳轉(zhuǎn)化成時(shí)間??
NSTimeInterval?interval=[timeStampString?doubleValue]?/?1000.0;??
NSDate?*date?=?[NSDate?dateWithTimeIntervalSince1970:interval];??
NSDateFormatter?*objDateformat?=?[[NSDateFormatter?alloc]?init];??
[objDateformat?setDateFormat:DateFormat];//年月日時(shí)分秒??
NSString?*?timeStr?=?[NSString?stringWithFormat:@"%@",[objDateformat?stringFromDate:?date]];??
return?timeStr;??
}??
#pragma mark - 算出當(dāng)前月份有多少天
-(NSUInteger)CalculateTheNumberOfdaysinthecurrentmonth:(NSDate*)date{
? ? NSCalendar*calendar = [NSCalendarcurrentCalendar];
? ? NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
? ? NSUIntegernumberOfDaysInMonth = range.length;
? ? returnnumberOfDaysInMonth;
}
http://blog.csdn.net/dashenid/article/details/77480248?
這個(gè)是找的別人寫的自定義的日期picherview? 一般我們都是 年月日 但是 如果只要年月比如: