关于 IOS 时间的一下用法

1. 加减月份

-(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
   
    [comps setMonth:month];
   
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
   
    return mDate;
   
}
 
2. 对日期进行排序
 // 对日期进行排序
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"yyyy年MM月dd日";
    _dataArray = [_dataPlanArray sortedArrayUsingComparator:^(NSString *string1, NSString *string2) {
        NSDate *date1 = [dateFormatter dateFromString:string1];
        NSDate *date2 = [dateFormatter dateFromString:string2];
       
        return [date1 compare:date2];
    }];
 
 
3. 时间戳转化为特定的字符串
+(NSString *)stringWithTimestamp:(NSTimeInterval)tt format:(NSString *)format
{
     NSDate *date = [NSDate dateWithTimeIntervalSince1970:tt];
     NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
     [formatter setDateFormat:format];
     return [formatter stringFromDate:date];
}
 
4. 时间转时间戳

 NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];  

 NSLog(@"timeSp:%@",timeSp); //时间戳的值

posted @ 2017-03-22 16:33  Dingzhijie  阅读(170)  评论(0编辑  收藏  举报