IOS時(shí)間的類的方法

/獲取年月日對(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? 一般我們都是 年月日 但是 如果只要年月比如:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末锯梁,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子耀怜,更是在濱河造成了極大的恐慌袜茧,老刑警劉巖爪瓜,帶你破解...
    沈念sama閱讀 212,332評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件页眯,死亡現(xiàn)場(chǎng)離奇詭異梯捕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)窝撵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,508評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門傀顾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人碌奉,你說我怎么就攤上這事短曾『” “怎么了?”我有些...
    開封第一講書人閱讀 157,812評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵错英,是天一觀的道長(zhǎng)入撒。 經(jīng)常有香客問我隆豹,道長(zhǎng)椭岩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,607評(píng)論 1 284
  • 正文 為了忘掉前任璃赡,我火速辦了婚禮判哥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘碉考。我一直安慰自己塌计,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,728評(píng)論 6 386
  • 文/花漫 我一把揭開白布侯谁。 她就那樣靜靜地躺著锌仅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪墙贱。 梳的紋絲不亂的頭發(fā)上热芹,一...
    開封第一講書人閱讀 49,919評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音惨撇,去河邊找鬼伊脓。 笑死,一個(gè)胖子當(dāng)著我的面吹牛魁衙,可吹牛的內(nèi)容都是我干的报腔。 我是一名探鬼主播,決...
    沈念sama閱讀 39,071評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼剖淀,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼纯蛾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起纵隔,我...
    開封第一講書人閱讀 37,802評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤翻诉,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后巨朦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體米丘,經(jīng)...
    沈念sama閱讀 44,256評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,576評(píng)論 2 327
  • 正文 我和宋清朗相戀三年糊啡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拄查。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,712評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡棚蓄,死狀恐怖堕扶,靈堂內(nèi)的尸體忽然破棺而出碍脏,到底是詐尸還是另有隱情,我是刑警寧澤稍算,帶...
    沈念sama閱讀 34,389評(píng)論 4 332
  • 正文 年R本政府宣布典尾,位于F島的核電站,受9級(jí)特大地震影響糊探,放射性物質(zhì)發(fā)生泄漏钾埂。R本人自食惡果不足惜浇坐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,032評(píng)論 3 316
  • 文/蒙蒙 一泼掠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧冬筒,春花似錦瞪慧、人聲如沸髓考。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽氨菇。三九已至,卻和暖如春妓湘,著一層夾襖步出監(jiān)牢的瞬間查蓉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,026評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工多柑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奶是,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,473評(píng)論 2 360
  • 正文 我出身青樓竣灌,卻偏偏與公主長(zhǎng)得像聂沙,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子初嘹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,606評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容