//獲得目前時間距1970的秒數
- (NSString *)setCurrTimeLenght
{
NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
NSString *timeLenght = [NSString stringWithFormat:@"%0.0f",time];
return timeLenght;
}
//根據距離1970年的秒數算給定秒數的日期時間
- (NSString *)getTimeString
{
NSString *strTime = [[NSString alloc]initWithString:self];
NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970: strTime.length >
([self setCurrTimeLenght].length+2)
? ([strTime longLongValue]/1000) : [strTime longLongValue]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //時間格式
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
strTime = [dateFormatter stringFromDate:mydate1970];
return strTime;
}
//擷取隻有時分秒
- (NSString *)getHoursMinutesString
{
NSString *strTime = [[NSString alloc]initWithString:self];
NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970: strTime.length > ([self setCurrTimeLenght].length+2) ? ([strTime longLongValue]/1000) : [strTime longLongValue]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];//@"yyyy-MM-dd HH:mm:ss"
[dateFormatter setDateFormat:@"HH:mm:ss"];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
strTime = [dateFormatter stringFromDate:mydate1970];
return strTime;
}
//擷取時分
- (NSString *)getHoursMinString
{
NSString *strTime = [[NSString alloc]initWithString:self];
NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970: strTime.length > ([self setCurrTimeLenght].length+2) ? ([strTime longLongValue]/1000) : [strTime longLongValue]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"HH:mm"];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
strTime = [dateFormatter stringFromDate:mydate1970];
return strTime;
}
轉載于:https://www.cnblogs.com/qwer-BHS/p/5316143.html