关于 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;
{
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];
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];
{
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); //时间戳的值
没有那个时代犹如现在,知识的爆发和技术的进步如此的迅速,必须不断的学习才能紧跟时代的步伐 . (PS:资料来源比较复杂 ,如有侵权 , 删)