iOS原生开发获取当前设备日期与时间

//获取当前时间
    NSDate *  timeDate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm:ss"];
    NSString *  locationString=[dateformatter stringFromDate:timeDate];
    NSLog(@"locationString is %@",locationString);
    
//    获取当前日期
    NSCalendar* cal = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit|NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDateComponents* conponent = [cal components:unitFlags fromDate:timeDate];
    NSInteger year = [conponent year];
    NSInteger month = [conponent month];
    NSInteger day = [conponent day];
    NSString* nsDateString = [NSString stringWithFormat:@"%4ld年%2ld月%2ld日",year,month,day];
    NSLog(@"nsDateString is %@",nsDateString);

你可能感兴趣的:(ios)