你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 時間類常用方法

iOS 時間類常用方法

編輯:IOS開發綜合

//當前日前日期

NSDate *today = [NSDate date];

 

//時區

NSTimeZone *zone = [NSTimeZone systemTimeZone];

 

//設置間隔

_interval = [zone secondsFromGMTForDate: today];

 

//今天+8小時(中國上海時區)

today = [today dateByAddingTimeInterval: _interval];

 

//獲取明天日期

NSDate *tomorrowDate = [[NSDate alloc]initWithTimeInterval:24 * 60 * 60 sinceDate:[NSDate date]];

 

//日歷

NSCalendar *calendar = [NSCalendar currentCalendar];

 

//comps裡可以分離出時間年月日等信息

 

NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit | NSHourCalendarUnit) fromDate:tomorrowDate];

 

//獲取當前星期幾

NSInteger weekday = [comps weekday];

 

//將NSString類型轉換成NSDate

 

NSDate *serverDate = [dateFomatter dateFromString:serverTimeStr];

NSDate *createDate = [dateFomatter dateFromString:createTimeStr];

 

//算出兩個Date的間隔

NSTimeInterval betweenTime = [serverDate timeIntervalSinceDate:createDate];


//分離出標准格式裡的年月日時分秒

 

NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];

[dateFomatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

 

//將string換成formatter形式

NSDate *stringDate = [dateFomatter dateFromString:string];

 

//formatter為月和日

[dateFomatter setDateFormat:@"MM-dd"];

 

//提取字符串中月和日

NSString *str1 = [dateFomatter stringFromDate:stringDate];


 


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved