iOS关于时间的处理看我就够了


一、字符串转化成日期:

NSString *str = @"2017-10-21 23:00:11 +0800";

// 1.创建一个时间格式化对象

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

// 2.格式化对象的样式

formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";

// 3.格式化让字符串转换成时间

NSDate *datestr = [formatter dateFromString:str];

// 4.自己给定的时间

NSDate *currentDate = datestr;

二、获取当月天数:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

//此处是查看当前月的天数

NSDate *timeDate = [NSDate date];

NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit: NSMonthCalendarUnit forDate:timeDate];

NSInteger countInteger = range.length;

NSLog(@"当月天数为:%lu",(unsigned long)countInteger);

三、获取指定日期是周几

NSDate *datestr = [NSDate date];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法

NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:datestr];

// 1:周日,2:周一 ,3.周二     ...以此类推

NSLog(@"今天是:%@",@([comps weekday]));

四、判断指定时间与当前时间大小

// 时间字符串

NSString *starTime= @"2017-23-20 11:10:05";

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

format.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSDate *currentTime = [format dateFromString:starTime];

// 当前时间

NSDate *nowTime = [NSDate date];

// 获得比较结果(谁大谁小)

NSComparisonResult result = [nowTime compare:currentTime];

if (result == NSOrderedAscending) {

// 升序, 越往右边越大

NSLog(@"创建时间大于当前时间");

} else if (result == NSOrderedDescending) {

// 降序, 越往右边越小

NSLog(@"创建时间小于当前时间");

} else {

NSLog(@"创建时间等于当前时间");

}

五、获取年月日星期时分秒

NSDate* date  = [NSDate date];

NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

 // NSDateComponent 可以获得日期的详细信息,即日期的组成

NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];

    NSLog(@"年 = year = %ld",comps.year);

    NSLog(@"月 = month = %ld",comps.month);

    NSLog(@"日 = day = %ld",comps.day);

    NSLog(@"时 = hour = %ld",comps.hour);

    NSLog(@"分 = minute = %ld",comps.minute);

    NSLog(@"秒 = second = %ld",comps.second);

    NSLog(@"星期 =weekDay = %ld ",comps.weekday);

//字符串类型  转化为  时间

NSString *string = @"1481706995000";

NSTimeInterval time = [string doubleValue] ;

NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];

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

dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSLog(@"转换的时间为:%@",[dateFormatter stringFromDate:date]);

输出:

转换的时间为:2016-12-14 17:56:35

记录两次请求前后时间差:

NSDate  *date1 = [NSDate date];

NSDate  *date2 = [NSDate date];

NSTimeInterval  a = [date2  timeIntervalSince1970]  -  [date1  timeIntervalSince1970];

NSLog(@"A与B之间相差%.3f秒",a);

时间戳  时间 互换

//时间戳 -> 时间

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

[tmpFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

[tmpFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];

NSDate *tmpTimedate = [NSDate dateWithTimeIntervalSince1970:tmpTime];

_mStrTime = [tmpFormatter stringFromDate:tmpTimedate];

参考:

+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{

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

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

[formatter setTimeZone:timeZone];

NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

NSLog(@"1296035591  = %@",confromTimesp);

NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

//NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);

return confromTimespStr;

}

你可能感兴趣的:(iOS关于时间的处理看我就够了)