获取当前时间

一  12进制

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

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[dateform setLocale:locale];

dateform.dateFormat = @"hhmmss";

NSString *dateStr = [dateform stringFromDate:[NSDate date]];

二  24进制

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

dateform.dateFormat = @"hhmmss";

NSString *dateStr = [dateform stringFromDate:[NSDate date]];

三  年月日 星期

- (NSString *)getYears{

NSArray * arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];

NSDate *date = [NSDate date];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:

NSCalendarIdentifierGregorian];

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

NSInteger unitFlags = NSCalendarUnitYear |

NSCalendarUnitMonth |

NSCalendarUnitDay |

NSCalendarUnitWeekday |

NSCalendarUnitHour |

NSCalendarUnitMinute |

NSCalendarUnitSecond;

comps = [calendar components:unitFlags fromDate:date];

NSInteger week = [comps weekday];

NSInteger year=[comps year];

NSInteger month = [comps month];

NSInteger day = [comps day];

NSString *dateStr=[NSString stringWithFormat:@"%ld年%ld月%ld日  %@",year,month,day,[arrWeek objectAtIndex:week-1]];

return dateStr;

}

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