zoukankan      html  css  js  c++  java
  • 计算某一天距离今天多少天,多少小时,多少分钟

    - (NSString *)intervalSinceNow: (NSString *) theDate
    {
        NSString *timeString=@"";
     
        NSDateFormatter *format=[[NSDateFormatter alloc] init];
        [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *fromdate=[format dateFromString:theDate];
        NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
        NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
        NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];
     
        //获取当前时间
        NSDate *adate = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: adate];
        NSDate *localeDate = [adate  dateByAddingTimeInterval: interval];
         
        double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];
        long lTime = fabs((long)intervalTime);
        NSInteger iSeconds =  lTime % 60;
        NSInteger iMinutes = (lTime / 60) % 60;
        NSInteger iHours = fabs(lTime/3600);
        NSInteger iDays = lTime/60/60/24;
        NSInteger iMonth =lTime/60/60/24/12;
        NSInteger iYears = lTime/60/60/24/384;
         
         
        NSLog(@"相差%d年%d月 或者 %d日%d时%d分%d秒", iYears,iMonth,iDays,iHours,iMinutes,iSeconds);
         
         
        if (iHours<1 && iMinutes>0)
        {
            timeString=[NSString stringWithFormat:@"%d分",iMinutes];
             
        }else if (iHours>0&&iDays<1 && iMinutes>0) {
            timeString=[NSString stringWithFormat:@"%d时%d分",iHours,iMinutes];
        }
        else if (iHours>0&&iDays<1) {
            timeString=[NSString stringWithFormat:@"%d时",iHours];
        }else if (iDays>0 && iHours>0)
        {
            timeString=[NSString stringWithFormat:@"%d天%d时",iDays,iHours];
        }
       else if (iDays>0)
        {
            timeString=[NSString stringWithFormat:@"%d天",iDays];
        }
        return timeString;
    }
  • 相关阅读:
    poj 2187 Beauty Contest (凸包)
    poj 1584 A Round Peg in a Ground Hole(计算几何)
    poj 1039 Pipe (计算几何)
    struts2开篇
    Action的内容
    输入校验
    文件上传
    Struts.xml内容
    .NET资源推荐:数据持久层
    线程间操作无效: 从不是创建控件“...”的线程访问它。
  • 原文地址:https://www.cnblogs.com/joesen/p/3540102.html
Copyright © 2011-2022 走看看