您的位置:首页 > 其它

关于时间的一些转化

2017-07-03 18:46 169 查看
#define FIELD_BASE_SECOND_AGO                           @"%d秒前"
#define FIELD_BASE_MINUTE_AGO                           @"%d分钟前"
#define FIELD_BASE_HOUR_AGO                             @"%d小时前"
#define FIELD_BASE_DAY_AGO                              @"%d天前"
#define FIELD_BASE_MONTH_AGO                            @"%d月前"
#define FIELD_BASE_YEAR_AGO                             @"%d年前"

#define differTime                 
30*60   
//单位:秒

@implementation
TimeUtil

//根据返回距离当前时间多长时间
+ (NSString*)caluteDateByCurrentDate:(NSDate*)date
{
   
NSString *timeStr =
nil;
   
   
//为了解决用户随便更改本地时间造成显示错误的问题
by cafei
   
//当本地时间小于creatTime的时候,小于半小时之内,显示一分钟前,否则显示creatTime
   
//当本地时间大于creatTime的时候,原样判断
   
if ([date
timeIntervalSinceNow] >
0)
    {
       
if ([date
timeIntervalSinceNow] >
differTime)
        {
           
//时间相差大于半个小时
           
NSDateFormatter *formatter = [[NSDateFormatter
alloc]
init];
            [formatter
setDateFormat:@"yyyy-MM-dd"];
            timeStr = [formatter
stringFromDate:date];
        }
       
else
        {
            timeStr = [NSString
stringWithFormat:FIELD_BASE_MINUTE_AGO,
1];
        }
    }
   
else
    {
       
NSCalendar *chineseCalendar = [[NSCalendar
alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
       
NSUInteger unitFlags =
NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|
       
NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
       
NSDateComponents *component = [chineseCalendar
components:unitFlags
fromDate:date
                                                          
toDate:[NSDate
date]
options:0];
       
if (component.year
!=
0)
        {
            timeStr = [NSString
stringWithFormat:FIELD_BASE_YEAR_AGO,(int)
component.year];
        }
       
else
if (component.month
!=
0)
        {
12353

            timeStr = [NSString
stringWithFormat:FIELD_BASE_MONTH_AGO,(int)
component.month];
        }
       
else
if (component.day
!=
0)
        {
            timeStr = [NSString
stringWithFormat:FIELD_BASE_DAY_AGO,
(int)component.day];
        }
       
else
if (component.hour
!=
0)
        {
            timeStr = [NSString
stringWithFormat:FIELD_BASE_HOUR_AGO,(int)
component.hour];
        }
       
else
if (component.minute
!=
0)
        {
            timeStr = [NSString
stringWithFormat:FIELD_BASE_MINUTE_AGO,(int)
component.minute];
        }
       
else
if (component.second
!=
0)
        {
            timeStr = [NSString
stringWithFormat:FIELD_BASE_SECOND_AGO,
(int)component.second];
        }
       
else
        {
           
NSDateFormatter *formatter = [[NSDateFormatter
alloc]
init];
            [formatter
setDateFormat:@"yyyy-MM-dd"];
            timeStr = [formatter
stringFromDate:date];
        }
    }
   
   
return timeStr;
}

//时分秒相互转换
+ (NSDictionary*)convertSecond2HourMinuteSecond:(int)second
{
   
NSMutableDictionary* dict = [[NSMutableDictionary
alloc]
init];
   
int hour =
0, minute =
0;
    hour = second /
3600;
    minute = (second - hour *
3600) /
60;
    second = second - hour *
3600
- minute * 
60;
    [dict
setObject:[NSNumber
numberWithInt:hour]
forKey:@"hour"];
    [dict
setObject:[NSNumber
numberWithInt:minute]
forKey:@"minute"];
    [dict
setObject:[NSNumber
numberWithInt:second]
forKey:@"second"];
   
return dict;
}

/**
 *

根据时间字符串返回总时长,传入参数格式必须为
"00:00:00"

样式
 */
+ (double)getVideoAllTimeWithTimeString:(NSString
*)timeString{
   
if (timeString ==
nil) {
       
return
0;
    }
   
if ([timeString
isEqualToString:@""])
{
       
return
0;
    }
   
NSArray *arr = [timeString
componentsSeparatedByString:@":"];
   
if (arr ==
nil) {
       
return
0;
    }
   
if (arr.count
<
1) {
       
return
0;
    }
   
double allTime =
0.0f;
   
for (int
i =
0; i < arr.count; i++) {
        allTime += [arr[i]
integerValue] *
pow(60,
arr.count
-i -
1);
    }
   
return allTime;
}

/**
 *

根据时间总时长返回时间字符串,返回的字符串为
"00:00:00"

样式
 */
+ (NSString
*)getVideoAllTimeStringWithVideoAllTime:(double)videoDuration orVideoAlltimeString:(NSString
*)timeString{
   
if (videoDuration >
0) {
       
long hour = videoDuration /
3600;
       
long minite = (videoDuration - hour *
3600) /
60;
       
long miao = videoDuration - hour *
3600
- minite *
60;
       
return [NSString
stringWithFormat:@"%02d:%02d:%02d",(int)hour,(int)minite,(int)miao];
    }else
if
(timeString){
       
if ([timeString
isEqualToString:@""])
{
           
return
nil;
        }
       
double allTime = [self
getVideoAllTimeWithTimeString:timeString];
       
long hour = allTime /
3600;
       
long minite = (allTime - hour *
3600) /
60;
       
long miao = allTime - hour *
3600
- minite *
60;
       
return [NSString
stringWithFormat:@"%02d:%02d:%02d",(int)hour,(int)minite,(int)miao];
    }
   
return
nil;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: