首页 > 代码库 > Objective-C日期相关工具方法
Objective-C日期相关工具方法
//date根据formatter转换成string+(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:formatter]; return[dateFormatter stringFromDate:date];}//string类型的时间戳转换成时间+(NSString*)dateStringToString:(NSString *)dateStr{ if ([dateStr isKindOfClass:[NSNull class]]) { return nil; } else { NSInteger time = [dateStr intValue]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:time]; NSString *timestr = [NSString stringWithFormat:@"%@",date]; NSRange rang; rang.location = 0;O rang.length = 10; NSString *needtime = [timestr substringWithRange:rang]; return needtime; }}//将日期从原格式转换成需要的格式+(NSString*)convertDateFormatter:(NSString*)sourceFormatter targetFormatter:(NSString*)targetFormatter dateString:(NSString*)dateString{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:sourceFormatter]; NSDate *date = [dateFormatter dateFromString:dateString]; [dateFormatter setDateFormat:targetFormatter]; return[dateFormatter stringFromDate:date];}//将日期字符串转换成date+(NSDate *)stringToDate:(NSString *)formatter dateString:(NSString *)dateString{ NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:formatter]; return [dateFormatter dateFromString:dateString];}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。