1.月份獲取
月份的獲取我相信很多人都弄過,獲取本月的月份
NSDate *newDate = [NSDate data];//這個就是本月的月份的信息
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSString *newStr = [formatter stringFromDate:newDate];//獲取到的就是月份的字符串形式
2.那如果獲取本月之前幾個月的月份呢,有人說很簡單呀,確實也是很簡單的。
首先我們看看一個的例子
NSTimeZone* timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8];//設置時區 這個很重要不設置的話默認就是美國的時間,會存在八個小時的誤差
[formatter setTimeZone:timeZone];
NSDate *fromDate = [formatter dateFromString:fromDateStr];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeInterval time1970 = [fromDate timeIntervalSince1970];//這個是計算1970年開始到現在的時間單位是s
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:dateTime];
NSInteger CuurentNumberOffDaysInMonth = range.length;//計算本月的天數
for (NSInteger i= 0 ; i<numberMonth; i++) {
NSDate *dateTime = [NSDate dateWithTimeIntervalSince1970:time1970+(86400*numberOffDaysInMonth)];
[formatter stringFromDate:dateTime];
NSString *string = [NSString stringWithFormat:@"%@",dateTime];
LYTimeModel *model = [[LYTimeModel alloc] init];
model.year = [string substringToIndex:4];
model.month = [string substringWithRange:NSMakeRange(5, 2)];
[tempDateS addObject:model];
}
3.在上述代碼中每個月的天數一直都跟本月的天數一樣,這裏就可以看出來了吧,每個月的天數怎麼可能一樣呢,下面就是我優化的部分。
NSTimeZone* timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8];//設置時區
[formatter setTimeZone:timeZone];
NSDate *fromDate = [formatter dateFromString:fromDateStr];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeInterval time1970 = [fromDate timeIntervalSince1970];
NSUInteger numberOffDaysInMonth = 0;
//計算當月的所有的天數
for (NSInteger i= 0 ; i<numberMonth; i++) {
NSDate *dateTime = [NSDate dateWithTimeIntervalSince1970:time1970+(86400*numberOffDaysInMonth)];
[formatter stringFromDate:dateTime];
NSString *string = [NSString stringWithFormat:@"%@",dateTime];
LYTimeModel *model = [[LYTimeModel alloc] init];
model.year = [string substringToIndex:4];
model.month = [string substringWithRange:NSMakeRange(5, 2)];
[tempDateS addObject:model];
//計算當月的所有的天數
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:dateTime];
NSInteger CuurentNumberOffDaysInMonth = range.length;
numberOffDaysInMonth += CuurentNumberOffDaysInMonth;
}
每一個月單獨計算當月的月份的天數這樣一來就可以準確計算出前幾個後幾個月份的的年月
4.最後送上另外一個計算某一個時間到另一個時間的年月日
說一個額外的小問題,跟本題無關,也是我自己使用時遇到的
NSString *path = [[NSBundle mainBundle] pathForResource:@"1080x1920.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
// NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
// 2.創建AVPlayerItem
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
// 3.創建AVPlayer
AVPlayer * player = [AVPlayer playerWithPlayerItem:item];
// 4.添加AVPlayerLayer
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
layer.frame = self.view.bounds;
[self.view.layer addSublayer:layer];
[player play];
上述是一個簡單的播放器功能,播放本地的MP4文件,但是你在XCode模擬器上運行這部分代碼,斷點會斷住,
會出現 :
Xcode模擬器得播放本地視頻崩潰總是報[AudioHAL_Client] AudioHardware.cpp:666:AudioObjectGetPropertyData: AudioObjectGetPropertyData: no object with given ID 0的問題,找不出問題來,但是在真機就可以正常運行了,沒有任何問題,後來查了一下資料,其實太簡單了
這裏不要有斷點,是不是很簡單。。。。。。,至於原因我猜想的事模擬器還是不是很完美的支持視頻播放導致的,只是純屬個人猜想的。。。