美文网首页
NSDate--时间

NSDate--时间

作者: coder_hong | 来源:发表于2016-06-08 14:33 被阅读2055次

获取时间

NSDate *date = [NSDate date];// 创建时间对象,获得当前时间

NSLog(@"%@", date);//打印出的时间是0 时区的时间(格林威治时间)(北京属于东8区)
转换为本地时间
// 创建一个当前的时间
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    // 获取时间间隔
    NSTimeInterval interval = [zone secondsFromGMTForDate:date];
    // 东八区时间
    NSDate *locaDate = [date dateByAddingTimeInterval:interval];
    NSLog(@"%@", locaDate)

当前时间---->时间戳

@interface NSDate (NSExtendedDate)

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
@property (readonly) NSTimeInterval timeIntervalSinceNow;
@property (readonly) NSTimeInterval timeIntervalSince1970;

1.首先获取0时区的时间转成东八区的时间

// 创建一个当前的时间
  NSDate *date = [NSDate date];
  NSTimeZone *zone = [NSTimeZone systemTimeZone];
  // 获取时间间隔
  NSTimeInterval interval = [zone secondsFromGMTForDate:date];
  // 东八区时间
  NSDate *locaDate = [date dateByAddingTimeInterval:interval];
  NSLog(@"%@", locaDate)

2.将获得的东八区时间转换成时间戳

// 获得时间戳
   // 获得时间戳
    NSTimeInterval interval1 = [locaDate timeIntervalSince1970]; // 距离1970
    NSTimeInterval inveral2 = [locaDate timeIntervalSinceNow]; // 当前时间
    NSLog(@"%f", interval1);

答应时间戳: 2016-06-08 13:52:59.845 test[14109:3160430] 1465393979.843557

时间戳------>时间

@interface NSDate (NSDateCreation)

+ (instancetype)date;
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;

案例

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:28799.998401];
    NSLog(@"%@", date);

时间的格式

注意:这里不需要将获取的时间转换时区

date转成日期格式的字符串
 // NSDate -->NSString
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *timeString = [formatter stringFromDate:date];
    NSLog(@"%@", timeString); 东八区时间字符串
将日期格式的字符串转换成Date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";//日期格式化类
    NSString *string = @"2015-4-16 16:42:31";//设置时间格式
    // 注意这里获取打印的时间是0时区的date  2015-04-16 08:42:31 +0000
    // 转换成东八区时间
    NSDate *date = [formatter dateFromString:string];//转化为NSDate时间
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger seconds = [zone secondsFromGMTForDate:date];
    NSDate *date8 = [date dateByAddingTimeInterval:seconds];
    NSLog(@"%@", date8); //  打印结果 2015-04-16 16:42:31 +00

NSDate常用的一些属性 方法

时间间隔
    NSDate *date = [NSDate date];
    NSDate *date2 = [NSDate dateWithTimeInterval : 5 sinceDate:date]; // 与当前时间相隔5秒
    NSTimeInterval seconds = [date timeIntervalSince1970]//从1970年到现在的秒数

//从现在开始的24小时

    NSTimeInterval secondsPerDay = 24*60*60;

    NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

    NSLog(@"tomorrow = %@",tomorrow);

//根据已有日期创建日期

        NSTimeInterval secondsPerDay1 = 24*60*60;

        NSDate *now = [NSDate date];

        NSDate *yesterDay = [now dateByAddingTimeInterval:-secondsPerDay1];

        NSLog(@"yesterDay = %@",yesterDay);
比较时间
//比较日期

        BOOL sameDate = [now isEqualToDate:yesterDay];

        NSLog(@"sameDate = %hhd",sameDate);

        //获取较早的日期

        NSDate *earlierDate = [yesterDay earlierDate:now];

        NSLog(@"earlierDate  = %@",earlierDate);

        //获取较晚的日期

        NSDate *laterDate = [yesterDay laterDate:now];

        NSLog(@"laterDate  = %@",laterDate);

        //两个日期之间相隔多少秒

        NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];

        NSLog(@"secondsBetweenDates=  %lf",secondsBetweenDates);

通过日历来创建时间

 //通过NSCalendar类来创建日期

        NSDateComponents *comp = [[NSDateComponents alloc]init];

        [comp setMonth:06];

        [comp setDay:12];

        [comp setYear:2015];

        NSCalendar *myCal= [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

        NSDate *myDate1 = [myCal dateFromComponents:comp];

        NSLog(@"myDate1 = %@",myDate1);

        //从已有日期获取日期

        unsigned units  = NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitYear;

        NSDateComponents *comp1 = [myCal components:units fromDate:now];

        NSInteger month = [comp1 month];

        NSInteger year = [comp1 year];

        NSInteger day = [comp1 day];

        //NSDateFormatter实现日期的输出

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

        [formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
 //或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];

        NSString *string = [formatter stringFromDate:now];

        NSLog(@"string = %@",string);

        NSLog(@"formater = %@",formatter);

        //用NSCalendar实现,计算某个时间C是某个日期B是相应时期A,中的第几(Hour?Day?Month?)

        NSCalendar *cal = [NSCalendar currentCalendar];

        NSLog(@"My calendar is %@",[cal calendarIdentifier]);//获取日期格式对象

        unsigned long daymonth = [cal ordinalityOfUnit:NSCalendarUnitDay//时间B(天)

                                           inUnit:NSCalendarUnitMonth//时间A(月)

                                          forDate:now];//时间C(当前时间)

        NSLog(@"This is day %lu of the month",daymonth);//当前时间C,是一个月(A)的第几天(B)

相关文章

  • NSDate--时间

    获取时间 转换为本地时间 当前时间---->时间戳 1.首先获取0时区的时间转成东八区的时间 2.将获得的东八区时...

  • NSDate--查询某天是星期几

    查询某天是星期几 查询某天是今天,明天,还是后天 比较两个时间谁早谁晚

  • 时间时间时间

    挤出一点点时间,随意记录一下。 有时深夜里偶尔想起的人甚至想念的人,想的不是脸,而是美好的记忆,感觉和好印象。或者...

  • 时间,时间,时间!

    夜深人静的时刻,才有空思考这几天接收到的潮水一般的信息: ICO被央行叫停了,当然,我个人投资的某一个项目也受到了...

  • 时间!时间!时间!

    巴菲特身家从100万变成540亿的秘密:真正帮你赚钱的是时间! “财务自由”是当下许多人梦寐以求的一种生活状态,简...

  • 时间,时间,时间!

    时间,时间,时间! 2017-04-03 昨天晚上抽时间把罗辑思维的《时间的朋友2016》2016年年底的文字又...

  • 时间时间时间

    板栗回到办公室就跟阿姨们胡拉拉去。拉拉到大家都下班了,她还要上彩泥课,看《数学小火车》,还要看动画片,一样没弄完都...

  • 时间! 时间!

    不知不觉在长投的小白班快要毕业了,这一段时间我的世界观有那么一些的颠覆,这一段旅途即将结束,却不像刚开始时候那么...

  • 时间! 时间!

    不知不觉在长投的小白班快要毕业了,这一段时间我的世界观有那么一些的颠覆,这一段旅途即将结束,却不像刚开始时候那么...

  • 时间,时间!

    死亡,让众生平等;时间,让生命独一无二。 网络游戏用打怪升级得到玩家的时间;老板用工资购买员工的时间;情侣之间的相...

网友评论

      本文标题:NSDate--时间

      本文链接:https://www.haomeiwen.com/subject/bxsjdttx.html