iOS 时间戳及其日期处理

获取系统当前时间

NSDate *  senddate=[NSDate date];
NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
//设置获取时间的格式
[dateformatter setDateFormat:@"yyyyMMddHHmmss"];

NSString *  locationString=[dateformatter stringFromDate:senddate];

时间戳转换为时间

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];//@"yyyy-MM-dd-HHMMss"
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
NSString* dateString = [formatter stringFromDate:date];

日期加N个月

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

[formatter setDateFormat:@"yyyy/MM/dd"];

NSDate *date = [formatter dateFromString:dateString];

[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents* dc1 = [[NSDateComponents alloc] init];
//需要加的月数
[dc1 setMonth:count];
NSDate *nextmonthDate = [ gregorian dateByAddingComponents:dc1 toDate:date options:0];

return [formatter stringFromDate:nextmonthDate];

工具类下载地址

里面包含其他日期转换的相关方法
戳我下载~~~

你可能感兴趣的:(iOS 时间戳及其日期处理)