1.0 获取系统时间和年月日 计算年龄
//获得系统时间
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"HH:mm"];
NSString * locationString=[dateformatter stringFromDate:senddate];
//[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
//NSString * morelocationString=[dateformatter stringFromDate:senddate];
//获得系统日期
NSCalendar * cal=[NSCalendar currentCalendar];
NSUInteger unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
NSInteger year=[conponent year];
NSInteger month=[conponent month];
NSInteger day=[conponent day];
NSString * nsDateString= [NSString stringWithFormat:@"%4d年%2d月%2d日",year,month,day];
[dateformatter release];
NSDate *senddate=[NSDate date];
NSCalendar *cal=[NSCalendar currentCalendar];
NSUInteger unitFlags=NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear;
NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
NSInteger year=[conponent year];
NSInteger birthYear = [[baseInforModel.birthday substringWithRange:NSMakeRange(0, 4)] integerValue];
NSInteger age = year - birthYear;
NSString *ageStr = [NSString stringWithFormat:@"%tu",age];
2.0 讲一个带时区的时间字符串转换格式成普通时间// 2016-03-04T08:26:33.831Z
// 好使
- (NSString *)htcTimeToLocationStr:(NSString*)strM
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd‘T‘HH:mm:ss.SSSZ"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
NSDate *dateFormatted = [dateFormatter dateFromString:strM];
[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
NSString *locationTimeString=[dateFormatter stringFromDate:dateFormatted];
return locationTimeString;
}
// 没试过这个
NSDateFormatter *format = [NSDateFormatter new];
[format setDateFormat:@"yyyyMMddHHmmss"];
[format setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
NSString *time = [format stringFromDate:[NSDate date]];
return time;
format 用你的字符串的format ,类似 yyyyMMddTHHmmss,记住你中间有个T,
然后再按时时区转下,上面代码是转成中国时区的,你换成GMT,应该就可以了