获取当前时间

获取当前时间

NSDate * senddate=[NSDate date];

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



[dateformatter setDateFormat:@"YYYY,MMdd"];



NSString *  locationString=[dateformatter stringFromDate:senddate];



NSLog(@"locationString:%@",locationString);

2.//获取当前时间

  NSDate *now = [NSDate date];

  NSLog(@"now date is: %@", now);



  NSCalendar *calendar = [NSCalendar currentCalendar];

  NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

  NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];



  NSInteger year = [dateComponent year];

  NSInteger month = [dateComponent month];

  NSInteger day = [dateComponent day];

  NSInteger hour = [dateComponent hour];

  NSInteger minute = [dateComponent minute];

  NSInteger second = [dateComponent second];



  NSLog(@"year is: %ld", year);

  NSLog(@"month is: %ld", month);

  NSLog(@"day is: %ld", day);

  NSLog(@"hour is: %ld", hour);

  NSLog(@"minute is: %ld", minute);

  NSLog(@"second is: %ld", second);

你可能感兴趣的:(获取当前时间)