1.关于日期的处理
获取前几天的时间,提取月日
NSArray *dayArry=@[@1,@2,@3,@4,@5,@6,@7];
NSDate*nowDate = [NSDate date];
NSDate* theDate;
for (NSNumber *days in dayArry ) {
NSInteger day=[days integerValue];
if (day!=0) {
NSTimeInterval oneDay = 24*60*60*1; //1天的长度
theDate = [nowDate initWithTimeIntervalSinceNow: -oneDay*day ];
}
else{
theDate = nowDate;
}
NSLog(@"%@",theDate);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:theDate];
NSUInteger numberOfDaysInMonth = range.length;
NSLog(@"%lu", (unsigned long)numberOfDaysInMonth);
unsigned unitFlags = NSYearCalendarUnit |NSMonthCalendarUnit |NSDayCalendarUnit;
NSDateComponents *components = [calendar components:unitFlags fromDate:theDate];
NSLog(@"%ld, %ld, %ld", (long)components.year, (long)components.month, (long)components.day);
NSString *dayString=[NSString stringWithFormat:@"%ld-%ld",(long)components.month,(long)components.day];
[self.LCView.xValues insertObject:dayString atIndex:0];
2.时间间隔
- (NSString *) compareCurrentTime:(NSString *)str
{
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *d=[date dateFromString:str];
NSTimeInterval late=[d timeIntervalSince1970]*1;
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval now=[dat timeIntervalSince1970]*1;
NSString *timeString=@"";
NSTimeInterval cha=late-now;
NSLog(@"%f",cha);
if (cha/3600<1) {
timeString = [NSString stringWithFormat:@"%f", cha/60];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"%@分", timeString];
}
if (cha/3600>1&&cha/86400<1) {
timeString = [NSString stringWithFormat:@"%f", cha/3600];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"%@小时", timeString];
}
if (cha/86400>1)
{
timeString = [NSString stringWithFormat:@"%.f",ceilf(cha/86400.0)+1];
// timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"%@", timeString];
}
else{
timeString=@"1";
}
NSString *timeString=[NSString stringWithFormat:@"%ld",[self getMondays:str]];
NSLog(@"%@",timeString);
return timeString;
}