OC_时间NSDate、日历NSCalendar

NSData

//在OC中,Foundation框架为我们提供了强大的时间操作类‘NSDate’,该类封装了各种处理时间和日期的API

//获取系统当前时间(GMT 格林尼治时间,与北京时间相差8小时 北京时间 = GMT + 8)

NSDate *date1 = [NSDate date];
NSLog(@"%@",date1);

//(NATimeInterval) 是一个以秒为单位的时间片,也叫时间撮

NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:3600*8];//北京时间
NSLog(@"%@",date2);

//距离1970.1.1日期tiimeInterval时间撮的时间

NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:120];//两分钟前

//距离sinceDate日期timeInterval时间撮的时间

NSDate *date4 = [NSDate dateWithTimeInterval:120 sinceDate:[NSDate date]];
NSLog(@"%@",date4);

//通过时间追加创建NSDate

NSDate *date5 = [date4 dateByAddingTimeInterval:120];
NSLog(@"%@",date5);

//日期比较
//1.两个日期的时间撮

NSTimeInterval timeIntervalSince1970 = [[NSDate date]timeIntervalSince1970];
NSLog(@"timeIntervalSince1970 = %f",timeIntervalSince1970);

NSTimeInterval timeIntervalSinceNow = [[NSDate date]timeIntervalSinceNow];
NSlog(@"timeIntervalSinceNow = %f",timeIntervalSinceNow);

NSDate *dateNow = [NSDate date];

NSDate *anhourago = [NSDate dateWithTimeInterval:-3600 sinceDate:dateNow];
NSLog(@"%@",anhourago);

//判断两个日期是否相等

bool flag = [dateNow isEqualToDate:date1];
if (flag) {
NSLog(@"dateNow = date1");
}
else{
NSLog(@"no");
}

    NSDate *date6 = [dateNow earlierDate:anhourago];
    if ([date6 isEqualToDate:anhourago]){
        NSLog(@"anhourago 是较早的时间");
    }
    else{
        NSLog(@"dateNow 是较早的时间");
    }

NSDate *date7 = [dateNow laterDate:anhourago];
if ([date7 isEqualToDate:anhourago]){
    NSLog(@"anhourago是较晚的时间");
}
else{
    NSLog(@"dateNow是较晚的时间");
}


NSComparisonResult comparisonResult = [dateNow compare:anhourago];
switch (comparisonResult){
    case NSOrderedAscending:{
        NSLog(@"dateNow < anchorage");
        break;
    }
    case NSOrderedSanme:{
        NSLog(@"=");
        break;
    }
    case NSOrderedDescending:{
        NSLog(@"dateNow > anhourago");
        break;      
    }
}

日历

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc]init];

components.year = 2016;
components.month = 8;
components.day=12;
components.hour=12+8;
components.minute=14;
components.second=55;

NSDate *date8 = [currentCalendar dateFromComponents:components];
NSLog(@"%@",date8);

//时区
//NSTimeZone
//列出所有的时区

NSArray *array1 =[NSTimeZone knownTimeZoneNames];
NSLog(@"%@",array);

//指定名称参数创建一个时区

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];

//指定时区缩写创建一个时区 UTC世界。。。什么鬼

NSTimeZone *timeZone2 = [NSTimeZone timeZoneWithAbbreviation:@"PRC"];

//如何将NSDate转换成字符串类型

NSDateFormatter *dateFormatter = [NSDateFormatter new];

//设置时区(默认时区)

dateFormatter.timeZone = [NSTimeZone defaultTimeZone];//本地时区

//设置时间输出格式

//[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];

NSString *string1 = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@",string1);

//如何将字符串转换成NSDate类型

NSString *string2 = @"1970-01-01 08:00:00";
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date9 = [dateFormatter dateFromString:string2];
NSLog(@"%@",date9);

NSTimeInterval timeInterval = date9.timeIntervalSinceNow;
NSLog(@"%f",-timeInterval);

NSTimeInterval timeInterval2 = [[NSDate date]timeIntervalSince1970];
NSLog(@"%f",timeInterval2);
/*

dateString 格式为:2016-08-08 18:00:00
传入时间 与现在时间 差距60秒内,方法输出 @“刚刚”
差距 1小时之内,输出@“xx分钟前”
差距 1-24小时以内 xx小时前
大于30天 xx月前
大于360天 完整日期

*/

NSString *str0 = @"2016-08-13 13:50:00";

Time *time = [[Time alloc]init];

[time timeDiff:str0];

//NSDate
//在OC中,Foundation框架为我们提供了强大的时间操作类‘NSDate’,该类封装了各种处理时间和日期的API

//转化的方法

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date1 = [dateFormatter dateFromString:date];

NSTimeInterval timeInterval1 = date1.timeIntervalSinceNow;



float a = -(timeInterval1/60);
int b;
NSLog(@"%f",-timeInterval1);

if (a < 1){
    NSLog(@"刚刚");
}
else if (a >= 1 && a< 60){
    NSLog(@"%d分钟前")
}
else if (a >= 60 &&a<60*24){
    NSLog(@"%d小时前",b);
}
else if (a >=60*24 && a<60*24*30){
    b = a/60/24;
    NSLog(@"%d天前",b);
}
else if(a >=60*24*30 && a<60*24*30*12){
    b = a/(60*24*30);
    NSLog(@"%d个月前",b);
}
else if (a>60*24*30*12){
    NSLog(@"%@",date);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值