OC中显示当前时间

   NSDate *date=[NSDate date];

        //创建了一个NSDATE对象 是一个0000时区的时间并不是我们当前的时间

        

  //获取当前时间

        NSTimeZone *timeZone=[NSTimeZone systemTimeZone];//返回一个系统的时区

        NSInteger interval=[timeZone secondsFromGMTForDate:date];

        //0时区是date 相差interval秒

        NSDate * localdate=[date dateByAddingTimeInterval:interval];

        NSLog(@"%@",localdate);

        //格式化输出

        NSDateFormatter *f=[NSDateFormatter new];

        f.dateFormat=@"yyyy年MM月dd日 HH时mm分ss秒";

        NSString * strdate=[f stringFromDate:localdate];

        NSLog(@"%@",strdate);

  NSCalendar * calendar  = [NSCalendar currentCalendar];//当前日历

  //  

  NSDateComponents * component  = [calendar components:NSCalendarUnitHour fromDate:date toDate:datelocal options:0];

    

    NSLog(@"%ld",component.hour);

//    NSInteger year = [calendar component:NSCalendarUnitMonth fromDate:date];

    

//    NSDateComponents * component = [calendar components:NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];

  

原文地址:https://www.cnblogs.com/DreamLinku/p/5385042.html