------------------------//类方法进行字符串转换时间方法实现。---------------------------------------------------------------------------
+(void)zhuanhuan:(NSString *)string{
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设置时间格式。此为24小时制
NSDate *date=[formatter dateFromString:string];//对字符串进行时间转换
NSDate *date1=[NSDate dateWithTimeInterval:-4*60*60 sinceDate:date];//将某个时间进行加减时间操作,时间格式会被自动改成12小时制。(以秒为单位。)
NSDate *date2=[date dateByAddingTimeInterval:-4*3600];//将某个时间进行加减时间操作,时间格式会被自动改成12小时制。
NSLog(@"%@",date2);//输出时间。
NSLog(@"%@",date1);//输出时间。
}
---------------------------------------------------------------------------------------------------------------------------
----------------------时间戳转化为时间格式-----------------------------------------------------------------------
时间戳转化为时间格式
//转换时间:
NSTimeInterval time=[[self.arrayModel[indexPath.row] valueForKey:@"publish_time"] doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
//[[NSDate date] timeIntervalSince1970];获取当前时间戳
----------------------------------------------------------------------------------------------------------------------
------------------------计算文字高度方法--------------------------------------------------------------------
//实现方法
-(CGSize)sizeofTextWithMaxSize:(CGSize)maxSize font:(UIFont *)font{
NSDictionary *attrs=@{NSFontAttributeName:font};
return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
//类方法
+(CGSize)sizeWithText:(NSString *)text maxSize:(CGSize)maxSize font:(UIFont *)font{
return [text sizeofTextWithMaxSize:maxSize font:font];
}
----------------------------------------------------------------------------------------------------------
--------------------------------------//计算文件大小--------------------------------------------------
- (NSInteger)fileSize
{
NSFileManager *mgr = [NSFileManager defaultManager];
// 判断是否为文件
BOOL dir = NO;
BOOL exists = [mgr fileExistsAtPath:self isDirectory:&dir];
// 文件\文件夹不存在
if (exists == NO) return 0;
if (dir) { // self是一个文件夹
// 遍历caches里面的所有内容 --- 直接和间接内容
NSArray *subpaths = [mgr subpathsAtPath:self];
NSInteger totalByteSize = 0;
for (NSString *subpath in subpaths) {
// 获得全路径
NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
// 判断是否为文件
BOOL dir = NO;
[mgr fileExistsAtPath:fullSubpath isDirectory:&dir];
if (dir == NO) { // 文件
totalByteSize += [[mgr attributesOfItemAtPath:fullSubpath error:nil][NSFileSize] integerValue];
}
}
return totalByteSize;
} else { // self是一个文件
return [[mgr attributesOfItemAtPath:self error:nil][NSFileSize] integerValue];
}
}
//删除文件夹
-(void)removeCache:(NSString *)chare
{
// NSString *chare=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES) lastObject];//示例路径
[_mgr removeItemAtPath:chare error:nil];//删除一个文件夹
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------判断某个时间是否为XX------------------------------------------------------------------------------------------------
/**
* 判断某个时间是否为今年
*/
- (BOOL)isThisYear
{
NSCalendar *calendar = [NSCalendar currentCalendar];
// 获得某个时间的年月日时分秒
NSDateComponents *dateCmps = [calendar components:NSCalendarUnitYear fromDate:self];
NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]];
return dateCmps.year == nowCmps.year;
}
/**
* 判断某个时间是否为昨天
*/
- (BOOL)isYesterday
{
NSDate *now = [NSDate date];
// date == 2014-04-30 10:05:28 --> 2014-04-30 00:00:00
// now == 2014-05-01 09:22:10 --> 2014-05-01 00:00:00
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
// 2014-04-30
NSString *dateStr = [fmt stringFromDate:self];
// 2014-10-18
NSString *nowStr = [fmt stringFromDate:now];
// 2014-10-30 00:00:00
NSDate *date = [fmt dateFromString:dateStr];
// 2014-10-18 00:00:00
now = [fmt dateFromString:nowStr];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:now options:0];
return cmps.year == 0 && cmps.month == 0 && cmps.day == 1;
}
/**
* 判断某个时间是否为今天
*/
- (BOOL)isToday
{
NSDate *now = [NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *dateStr = [fmt stringFromDate:self];
NSString *nowStr = [fmt stringFromDate:now];
return [dateStr isEqualToString:nowStr];
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------