iOS 关于时间、日期的各种计算

本文介绍如何使用Objective-C进行时间日期的比较与计算,包括时间大小的对比、计算两日期之间的间隔、获取星期几以及日期加减天数的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个时间比较大小(只知道大小结果)

       NSDate *date1=[df dateFromString:time1];

        NSDate *date2=[dfdateFromString:time2];

       switch ([date1 compare:date2]) {

           case NSOrderedSame:

                break;

            caseNSOrderedAscending:

                break;

            caseNSOrderedDescending:

                break;

           default:

               break;

        }


两个时间、日期比较(具体相差多少时间)

-(NSString*)returnUploadTime:(NSString *)compareStr

{

    NSDateFormatter *date=[[NSDateFormatteralloc] init];

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

   NSDate *compareDate = [date dateFromString:compareStr];

    

   NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];

   // timeInterval = -timeInterval;

   int temp = 0;

   NSString *result;

   if (timeInterval < 60) {

        result = [NSStringstringWithFormat:@"距离此任务完成不到1分钟"];

    }

   else if((temp = timeInterval/60) <60){

        result = [NSStringstringWithFormat:@"距离此任务完成还有%d分钟",temp];

    }

    

   else if((temp = temp/60) <24){

        result = [NSStringstringWithFormat:@"距离此任务完成还有%d小时",temp];

    }

    

   else if((temp/24) <30){

       int hour = temp%24;

        temp = temp/24;

        result = [NSStringstringWithFormat:@"距离此任务完成还有%d%d小时",temp,hour];

    }

    

   else if((temp = temp/30) <12){

        result = [NSStringstringWithFormat:@"%d",temp];

    }

   else{

        temp = temp/12;

        result = [NSStringstringWithFormat:@"%d",temp];

    }

    

   return  result;

}



根据日期计算星期几

-(NSString*)weekDay:(NSDate*)udate

{

    NSCalendar* calendar = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents* components = [[NSDateComponentsalloc] init];

    

    NSInteger unitFlags =NSYearCalendarUnit |NSMonthCalendarUnit |NSDayCalendarUnit |NSWeekdayCalendarUnit |NSHourCalendarUnit |NSMinuteCalendarUnit |NSSecondCalendarUnit;

    

    components =[calendarcomponents:unitFlags fromDate:udate];

    

   NSInteger weekday = [components weekday];

   NSArray *chWeekDayArray = [[NSArrayalloc] initWithObjects:@"",@"",@"",@"",@"",@"",@"",nil];

    

   return [chWeekDayArray objectAtIndex:weekday-1];

}



日期和天数的计算

 NSDateFormatter *format = [[NSDateFormatteralloc]init];

    [formatsetDateFormat:@"yyyy-MM-dd"];

   NSDate *oneDate = [format dateFromString:startdate];

   NSTimeInterval oneSe = [oneDate timeIntervalSince1970];

   NSTimeInterval anotherSe = days*24*60*60;

   NSTimeInterval allSe = oneSe+anotherSe;

   NSDate*confromTimesp = [NSDatedateWithTimeIntervalSince1970:allSe];

   NSString*confromTimespStr = [format stringFromDate:confromTimesp];

   return confromTimespStr;



to be continue 。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值