OC_07_02 NSDate

在main函数里
#import <Foundation/Foundation.h>
#import "Tool.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //NSDate
        //在OC中,Foundation框架为我们提供了强大的时间操作类'NSDate',该类封装了各种处理时间和日期的API
        
        //获取系统当前时间(GMT Greenwich Mean Time 格林尼治标准时间,它与北京时间相差8个小时,北京时间 = GMT时间+8)
        NSDate *date1 = [NSDate date];
        NSLog(@"%@",date1);
        
        //当前系统时间+100秒
        //NSTimeInterval 是以秒为单位的时间片,也叫做时间戳
        NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:60];
        NSLog(@"%@",date2);
        
        //1970年1月1日0时+0秒
        NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:0];
        NSLog(@"%@",date3);
        
        //距离sinceDate日期TimeInterval时间的时间
        NSDate *date4 = [NSDate dateWithTimeInterval:100 sinceDate:[NSDate date]];
        NSLog(@"%@",date4);
    
        //通过时间追加创建NSDate,同样的返回seconds秒后的时间,如果这个seconds为负的话,就是seconds秒前的时间
        NSDate *date5 = [date4 dateByAddingTimeInterval:100];
        NSLog(@"%@",date5);
        
        //日期比较
        //1.两个日期的时间间隔
        //将一个日期转换成时间戳,此时间戳是以1970年为标准
        NSTimeInterval timrInterval1970 =  [[NSDate date] timeIntervalSince1970];
        NSLog(@"%.1f",timrInterval1970);
        
        //将一个时间转换成时间戳,此时间戳是以现在为标准
        NSTimeInterval timrIntervalNow = [[NSDate date] timeIntervalSinceNow];
        NSLog(@"%f",timrIntervalNow);
        
        //2.日期的早晚比较
        NSDate *dateNow = [NSDate date];
        NSDate *anHourAgo = [NSDate dateWithTimeIntervalSinceNow:-3600];
        BOOL flag = [dateNow isEqualToDate:anHourAgo];
        if (flag)
        {
            NSLog(@"相等");
        }
        
        //判断两个日期先后,返回较早的日期
        NSDate *date6 = [dateNow earlierDate:anHourAgo];
        if ([date6 isEqualToDate:dateNow])
        {
            NSLog(@"较早的时间是dateNow");
        }
        else
        {
            NSLog(@"较早的时间是anHourAgo");
        }
        
        //判断两个日期的先后,返回较晚的日期
        NSDate *date7 = [dateNow laterDate:anHourAgo];
        if ([date7 isEqualToDate:dateNow])
        {
            NSLog(@"较晚的时间是dataNow");
        }
        else
        {
            NSLog(@"较晚的时间是anHourAge");
        }
        
        //比较两个日期的早晚,也可以使用我们之前学过的 compare
        NSComparisonResult comparisonResult = [dateNow compare:anHourAgo];
        switch (comparisonResult)
        {
            case NSOrderedAscending:
            {
                NSLog(@"dateNow < anHourAgo");
            }
                break;
            case NSOrderedSame:
            {
            NSLog(@"dateNow = anHourAgo");
            }
                break;
            case NSOrderedDescending:
            {
            NSLog(@"dateNow > anHourAgo");
            }
                break;
            default:
                break;
        }
        
        
        /*
        NSCalendar
         我们通过NSTimeInterbal 创建一个时间很方便,但很不直观,Foundation框架为此为我们提供了NSCalendar,NSCalendar由更自然的日期组成,例如日,月,星期等,可以使用NSDateComponents 和 NSCalender 来创建一个NSDate对象
        */
        NSCalendar *currentCalendar = [NSCalendar currentCalendar];
        
        NSDateComponents *components = [[NSDateComponents alloc] init];
        
        components.year = 2015;
        components.month = 12;
        components.day = 17;
        components.hour = 22;
        components.minute = 46;
        components.second = 30;
        
        //输出日期的小时减8了
        NSDate *datee = [currentCalendar dateFromComponents:components];
        NSLog(@"%@",datee);
        
        //NSTimeZone
        //处理日期和时间经常遇到的一个问题,就是时区问题.Foundation框架提供了NSTimeZone来指定地区日历对象的时区
        
        //列出所有时区
        NSArray *arr = [NSTimeZone knownTimeZoneNames];
        NSLog(@"%@",arr);
        
        //可以指定名称参数创建一个时区
        NSTimeZone *timeZone1 = [NSTimeZone timeZoneWithName:@"Asia/Tokyo"];
        
        //可以指定时区缩写创建一个时区
        NSTimeZone *timeZone2 = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
        
        //如何将NSDate 转换成NSString ? NSDateFormatter默认转换为本地区时间
        NSDateFormatter *dateFormatter = [NSDateFormatter new];
        
        //设置时区
        dateFormatter.timeZone = [NSTimeZone systemTimeZone];
        
        //设置时间输出格式
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//        [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
        
        NSString *string = [dateFormatter stringFromDate:[NSDate date]];
        NSLog(@"%@",string);
        
        //将时间的类型的字符串转换成日期
        NSString *string1 = @"1970-01-01 08:00:00";
        
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        //这里得到的时间是GMT时间
        NSDate *date8 = [dateFormatter dateFromString:@"2015-12-16 16:46:49"];
        NSLog(@"%@",date8);
        
        
        NSTimeInterval timeInterval = date8.timeIntervalSinceNow;
        NSLog(@"距今已经%f秒了",-timeInterval/3600);
//
//        NSLog(@"%f",[[NSDate date ] timeIntervalSince1970]);
//
        
        
        NSString *str = [Tool handleDate:@"2014-12-30 9:01:12"];
        NSLog(@"%@",str);        
    }
    return 0;
}

在Tool.h文件里

//dateString 格式为:2015-12-17 08:08:08
+(NSString *)handleDate:(NSString *)dateString;

/*
 传入时间与现在时间  差距 60 秒以内,输出@"刚刚"
                  差距 1 个小时以内,输出@"xx分钟前"
                  差距 1-24 小时以内 输出@"xx小时前"
                  差距 大于一天 输出@"xx天前"
                  差距 大于30天 输出@"xx个月前"
                  差距 大于365天 输出@"完整日期"
 */


在Tool.m文件里


@implementation Tool

+(NSString *)handleDate:(NSString *)dateString
{
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    dateFormatter.timeZone = [NSTimeZone systemTimeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    
    NSString *str = [[NSString alloc] initWithString:dateString];
    NSDate *dateD = [dateFormatter dateFromString:str];
    
    NSTimeInterval time = dateD.timeIntervalSinceNow/60;
    if (-time<1)
    {
        return @"刚刚";
    }
    if (-time>1 && -time<60)
    {   
        NSString *str1 = [NSString stringWithFormat:@"%0.f",-time];
        NSString *str2 = @"分钟前";
        NSString *str3 = [NSString stringWithFormat:@"%@%@",str1,str2];
        return str3;
    }
    if (-time >60 && -time < 1440)
    {
        NSString *str4 = [NSString stringWithFormat:@"%0.f",-time/60];
        NSString *str5 = @"小时前";
        NSString *str6 = [NSString stringWithFormat:@"%@%@",str4,str5];
        return str6;
    }
    if (-time > 1440 && -time <43200)
    {
        NSString *str7 = [NSString stringWithFormat:@"%0.f",-time/60/24];
        NSString *str8 = @"天前";
        NSString *str9 = [NSString stringWithFormat:@"%@%@",str7,str8];
        return str9;
    }
    if (-time>43200 && -time<518400)
    {
        NSString *str10 = [NSString stringWithFormat:@"%0.f",-time/60/24/30];
        NSString *str11 = @"月前";
        NSString *str12 = [NSString stringWithFormat:@"%@%@",str10,str11];
        return str12;
    }
    if (-time>518400)
    {
         return dateString;
    }
    return 0;

}
//差距 60 秒以内,输出@"刚刚"
//差距 1 个小时以内,输出@"xx分钟前"
//差距 1-24 小时以内 输出@"xx小时前"
//差距 大于一天 输出@"xx天前"
//差距 大于30天 输出@"xx个月前"
//差距 大于365天 输出@"完整日期"
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值