常用方法
日期
- 獲取當(dāng)前日期
[NSDate date] - 獲取當(dāng)前日期開始,若干秒后的日期
[NSDate dateWithTimeIntervalSinceNow:3600.0*10] - 某個(gè)日期開始,若干秒后的日期
[date1 dateByAddingTimeInterval:60] - 從1970年開始,經(jīng)過了若干秒的時(shí)間
[NSDate dateWithTimeIntervalSince1970:1000]
間隔
- 從1970年到某一時(shí)間的秒數(shù)
[date timeIntervalSince1970] - 計(jì)算二個(gè)日期的間隔
[date1 timeIntervalSinceDate:date] - 某個(gè)日期到現(xiàn)在的間隔
[date1 timeIntervalSinceNow]
擴(kuò)展方法
- 時(shí)間戳轉(zhuǎn)指定格式的日期
<pre> + (NSString *)secondTransYearMonther:(NSString *)time isAddHour:(BOOL)isAdd
{
NSTimeInterval _interval = [time doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
if (isAdd) {
[objDateformat setDateFormat:@"yyyy年MM月dd日 hh:mm:ss"];
}
else{
[objDateformat setDateFormat:@"yyyy年MM月dd日"];
}
NSString *temp = [objDateformat stringFromDate:date];
return temp;
}
</pre> - 字符串和日期互相轉(zhuǎn)換
<pre>
此時(shí)需要稍加注意formater格式要與字符串格式完全一致,否則轉(zhuǎn)換失敗。
</pre> - 星期幾的獲取
<pre>+ (NSString *)weekdayStringFromDate:(NSDate*)inputDate
{
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
</pre>
<pre>
根據(jù)項(xiàng)目的需要內(nèi)容會持續(xù)擴(kuò)充
</pre>