zoukankan      html  css  js  c++  java
  • 时间差

                                                        时间差

    -(NSString *) returnUploadTime:(NSString *)timeStr

    {

        //创建一种时间格式

        NSDateFormatter *date=[[NSDateFormatter alloc] init];

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

        //将字符串格式化

        NSDate *d=[date dateFromString:timeStr];

        //返回以1970/01/01 GMT为基准,然后过了secs秒的时间

        NSTimeInterval late=[d timeIntervalSince1970]*1;

        //返回以当前时间为基准,然后过了secs秒的时间

        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

        //获取到了当前时刻距离GMT基准的总秒数

        NSTimeInterval now=[dat timeIntervalSince1970]*1;

        NSString *timeString=@"";

        //得到时间差

        NSTimeInterval cha=now-late;

        //时间差小于一小时的时候的显示格式

        if (cha/3600<1) 

        {

            timeString = [NSString stringWithFormat:@"%f", cha/60];

            timeString = [timeString substringToIndex:timeString.length-7];

            timeString = [NSString stringWithFormat:@"%@分钟前", timeString];

        }

        //时间差在一个小时到一天时的显示格式

        if (cha/3600>1&&cha/86400<1) 

        {

            NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

            [dateformatter setDateFormat:@"HH”];

            timeString = [NSString stringWithFormat:@“%@个小时前”,[dateformatter stringFromDate:d]];

        }

        //时间差大于一天时的显示格式

        if (cha/86400>1)

        {

            NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

            [dateformatter setDateFormat:@"dd"];

            timeString = [NSString stringWithFormat:@“%@天前”,[dateformatter stringFromDate:d]];

        }

        return timeString;

    }

    Objective-CNSDate详解及获取当前时间等常用操作

    NSDate类用于保存时间值,同时提供了一些方法来处理一些基于秒级别时差(Time Interval)运算和日期之间的早晚比较等。

    1. 创建或初始化可用以下方法

    用于创建NSDate实例的类方法有

    + (id)date;

    返回当前时间

    + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;  

    返回以当前时间为基准,然后过了secs秒的时间

    + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

    返回以2001/01/01 GMT为基准,然后过了secs秒的时间

    + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

    返回以1970/01/01 GMT为基准,然后过了secs秒的时间

    + (id)distantFuture;

    返回很多年以后的未来的某一天。(比如你需要一个比现在(Now)晚(大)很长时间的时间值,则可以调用该方法。测试返回了4000/12/31 16:00:00)

    + (id)distantPast;

    返回很多年以前的某一天。(比如你需要一个比现在(Now)早(小)大很长时间的时间值,则可以调用该方法。测试返回了公元前0001/12/31 17:00:00)

    用于创建NSDate实例的实例方法有

    - (id)addTimeInterval:(NSTimeInterval)secs;

    返回以目前的实例中保存的时间为基准,然后过了secs秒的时间

    用于初始化NSDate实例的实例方法有

    - (id)init;

    初始化为当前时间。类似date方法

    初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似dateWithTimeIntervalSinceReferenceDate:方法

    - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;

    初始化为以refDate为基准,然后过了secs秒的时间

    - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

    初始化为以当前时间为基准,然后过了secs秒的时间

    2. 日期之间比较可用以下方法

    - (BOOL)isEqualToDate:(NSDate *)otherDate;

    与otherDate比较,相同返回YES

    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较早的那个日期

    - (NSDate *)laterDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较晚的那个日期

    - (NSComparisonResult)compare:(NSDate *)other;

    该方法用于排序时调用:

    . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame

    . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

    . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

    3. 取回时间间隔可用以下方法

    - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;

    以refDate为基准时间,返回实例保存的时间与refDate的时间间隔

    - (NSTimeInterval)timeIntervalSinceNow;

    以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔

    - (NSTimeInterval)timeIntervalSince1970;

    以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔

    - (NSTimeInterval)timeIntervalSinceReferenceDate;

    以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔

    + (NSTimeInterval)timeIntervalSinceReferenceDate;

    以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔

    4. 将时间表示成字符串

    - (NSString *)description;

    以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。(其中 "±HHMM" 表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则 "±HHMM" 显示为 "+0800")

    http://www.cnblogs.com/PaulpauL/ 版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    Qt——组件位置随窗口变化
    (转) Qt 出现“undefined reference to `vtable for”原因总结
    在CentOS上安装和部署Shiny Server
    Hibernate实体生成JSON的问题及解决
    在CentOS上安装并运行SparkR
    CentOS 6主机上的RStudio Server安装步骤
    Oracle用户密码过期后重置SYS用户密码
    在CentOS中将/var等已有目录挂载到新添加的硬盘
    CentOS中的常用命令
    Java在Web项目中读取properties文件
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/4935272.html
Copyright © 2011-2022 走看看