。h
//
// TimeOperation.h
// gtg1.0
//
// Created by linpeng on 14-2-28.
// Copyright (c) 2014年 lijx. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TimeOperation : NSObject
#pragma mark 距离当前时间 返回距离当前 是多少天前/后
+(NSString *)RemainingCarlend:(NSString *)startTime;
#pragma mark 距离当前时间 返回距离当前 多少时间
+(NSString *)RemainingTime:(NSString *)startTime;
#pragma mark 距离当前时间 返回距离当前 多少时间 2014-02-28 11:12:13 return 11:12
+(NSString *)SubCurrentTime:(NSString *)currenTime;
#pragma mark - 字符串转时间
+(NSDate *) stringToDate:(NSString *)dateStr;
#pragma mark - 时间转字符串
+(NSString *) dateToString:(NSDate *)date vFormate:(NSString *)formate;
#pragma mark - 获取两个时间差
+(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate;
#pragma mark - 获取两个时间差
+(NSInteger)daysWithinEraFromString:(NSString *) startDate toDate:(NSString *) endDate;
#pragma mark - 获取两个时间差
+(float)hoursWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate;
#pragma mark - 获取两个时间差
+(float)hoursWithinEraFromString:(NSString *) startDate toDate:(NSString *) endDate;
#pragma mark - 时间戳转字符串时间
+(NSString *)TimeStampChangeToStr:(NSString *)date;
#pragma mark - 时间戳转字符串时间(对应格式)
+(NSString *)TimeStampChangeToStrWithFormate:(NSString *)date vFormate:(NSString *)formate;
@end
。m
//
// TimeOperation.m
// gtg1.0
//
// Created by linpeng on 14-2-28.
// Copyright (c) 2014年 lijx. All rights reserved.
//
#import "TimeOperation.h"
@implementation TimeOperation
#pragma mark 距离当前时间 返回距离当前 多少时间 2014-02-28 11:12:13 return 11:12
+(NSString *)SubCurrentTime:(NSString *)currenTime
{
@try {
return [currenTime substringWithRange:NSMakeRange(11, 5)];
}
@catch (NSException *exception) {
return @"未定义";
}
}
#pragma mark 距离当前时间 返回距离当前 多少时间
+(NSString *)RemainingTime:(NSString *)startTime
{
NSDate *now=[NSDate date];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; //默认转换为本时区时间
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //设置日期的格式及时间
NSString *str=[dateFormatter stringFromDate:now]; //将NSDate转化为string
NSString *dateContent;
//创建了两个日期对象
NSDate *date1=[dateFormatter dateFromString:startTime];
NSDate *date2=[dateFormatter dateFromString:str];
NSTimeInterval time=[date1 timeIntervalSinceDate:date2];
if(time<0)
{
dateContent = @"结束";
return dateContent;
}
if (time/3600.0<1) {
dateContent = [NSString stringWithFormat:@"%f", time/60];
dateContent = [dateContent substringToIndex:dateContent.length-7];
dateContent=[NSString stringWithFormat:@"%@分钟后", dateContent];
}
if (time/3600.0>1&&time/86400.0<1) {
dateContent = [NSString stringWithFormat:@"%f", time/3600];
dateContent = [dateContent substringToIndex:dateContent.length-7];
dateContent=[NSString stringWithFormat:@"%@小时后", dateContent];
}
if (time/86400.0>1)
{
dateContent = [NSString stringWithFormat:@"%f", time/86400];
dateContent = [dateContent substringToIndex:dateContent.length-7];
dateContent=[NSString stringWithFormat:@"%@天", dateContent];
} return dateContent;
}
#pragma mark 距离当前时间 返回距离当前 是多少天前/后
+(NSString *)RemainingCarlend:(NSString *)startTime
{
NSDate *now=[NSDate date];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; //默认转换为本时区时间
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //设置日期的格式及时间
NSString *str=[dateFormatter stringFromDate:now]; //将NSDate转化为string
NSString *dateContent;
//创建了两个日期对象
NSDate *date1=[dateFormatter dateFromString:startTime];
NSDate *date2=[dateFormatter dateFromString:str];
NSTimeInterval time=[date1 timeIntervalSinceDate:date2];
int days =time/86400;
if(days>=0)
{
if (time/3600>1&&time/86400<1)//不超过24小时的 标注未明天
{
return @"明天";
}
switch (days) {
case 0:
dateContent=@"今天";
break;
case 1:
dateContent=@"明天";
break;
case 2:
dateContent=@"后天";
break;
default:
dateContent=[NSString stringWithFormat:@"%d天后",days];
break;
}
}
else
{
switch (days) {
default:
dateContent=[NSString stringWithFormat:@"%d天前",0-days];
break;
}
}
return dateContent;
}
#pragma mark - 获取两个时间差 天
+(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
int days = [comps day];
return days;
}
#pragma mark - 字符串转时间
+(NSDate *) stringToDate:(NSString *)dateStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateStr];
return date;
}
#pragma mark - 时间转字符串
+(NSString *) dateToString:(NSDate *)date vFormate:(NSString *)formate
{
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:formate];
//用[NSDate date]可以获取系统当前时间
return [dateFormatter stringFromDate:date];
}
#pragma mark - 获取两个时间差 天
+(NSInteger)daysWithinEraFromString:(NSString *) startDate toDate:(NSString *) endDate;
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:[self stringToDate:startDate] toDate:[self stringToDate:endDate] options:0];
int days = [comps day];
return days;
}
#pragma mark - 获取两个时间差 小时
+(float)hoursWithinEraFromString:(NSString *) startDate toDate:(NSString *) endDate;
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; //默认转换为本时区时间
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //设置日期的格式及时间
NSDate *date1=[dateFormatter dateFromString:startDate];
NSDate *date2=[dateFormatter dateFromString:endDate];
NSTimeInterval time=[date2 timeIntervalSinceDate:date1];
float hours = time/3600.0;
return hours;
}
#pragma mark - 获取两个时间差 小时
+(float)hoursWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
NSTimeInterval time=[endDate timeIntervalSinceDate:startDate];
float hours = time/3600.0;
return hours;
}
#pragma mark - 时间戳转字符串时间
+(NSString *)TimeStampChangeToStr:(NSString *)date
{
if (![date isKindOfClass:[NSString class]]) {
date = [NSString stringWithFormat:@"%@", date];
}
NSLog(@"%d",date.length);
NSString *lastActTime = @"暂无";
if (date&&date.length>=13) {
long long data = [date longLongValue]/1000;
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:data];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//用[NSDate date]可以获取系统当前时间
lastActTime = [dateFormatter stringFromDate:confromTimesp];
}else
{
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[date longLongValue]];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//用[NSDate date]可以获取系统当前时间
lastActTime = [dateFormatter stringFromDate:confromTimesp];
}
return lastActTime;
}
#pragma mark - 时间戳转字符串时间(对应格式)
+(NSString *)TimeStampChangeToStrWithFormate:(NSString *)date vFormate:(NSString *)formate
{
if (![date isKindOfClass:[NSString class]]) {
date = [NSString stringWithFormat:@"%@", date];
}
NSLog(@"%d",date.length);
NSString *lastActTime = @"暂无";
if (date&&date.length>=13) {
long long data = [date longLongValue]/1000;
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:data];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:formate];
//用[NSDate date]可以获取系统当前时间
lastActTime = [dateFormatter stringFromDate:confromTimesp];
}else
{
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[date longLongValue]];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:formate];
//用[NSDate date]可以获取系统当前时间
lastActTime = [dateFormatter stringFromDate:confromTimesp];
}
return lastActTime;
}
@end