NSDate--时间

获取时间

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--时间)