服务器返回字符串或者nsdate类型 时 转成 产品要求的 多少分钟之前, 几天前之类的时间字符串
//应用实例
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:@"按钮1" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn1.frame = CGRectMake(20, 100, 100, 30);
[btn1 addTarget:self action:@selector(clickBtn1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)clickBtn1:(UIButton *)btn
{
NSString *str = [self compareCurrentStrTime:@"2017-05-27 17:30:30"];
NSLog(@"时间:%@",str);
}
//服务器返回字符串或者nsdate类型 时 转成 产品要求的 多少分钟之前, 几天前之类的时间字符串
- (NSString *)compareCurrentStrTime:(NSString*) compareStr
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* inputDate = [inputFormatter dateFromString: compareStr];
return [self compareCurrentTime:inputDate];
}
/**
* 计算指定时间与当前的时间差
* @param compareDate 某一指定时间
* @return 多少(秒or分or天or月or年)+前 (比如,3天前、10分钟前)
*/
- (NSString *)compareCurrentTime:(NSDate*) compareDate
//
{
NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow];
timeInterval = -timeInterval;
long temp = 0;
NSString *result;
if (timeInterval < 60) {
result = [NSString stringWithFormat:@"刚刚"];
}
else if((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%ld分钟前",temp];
}
else if((temp = temp/60) <24){
result = [NSString stringWithFormat:@"%ld小时前",temp];
}
else if((temp = temp/24) <30){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
else if((temp = temp/30) <12){
result = [NSString stringWithFormat:@"%ld月前",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
}
//常用的字符串与时间戳转换方法
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* inputDate = [inputFormatter dateFromString:@"yyyy-MM-dd HH:mm:ss"只能转换这种样式的时间字符串];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM月dd日"];
NSString *dateString = [dateFormatter stringFromDate:inputDate];
//可能有人对NSDate不了解
NSLog(@"kVCSet开始时间-->%@",[NSDate date]);
打印结果为 : kVCSet开始时间-->2017-05-27 10:12:21 +0000
//实例
+ (NSString *)dateFormatter:(NSString *)formatterStr date:(NSDate *)date{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:formatterStr];
return [dateFormatter stringFromDate:date];
}
+ (NSDate *)dateStr:(NSString *)dateStr dateFormatter:(NSString *)formatterStr{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:formatterStr];
return [dateFormatter dateFromString:dateStr];
}
//不乱传进去@"1988-09-08" 或者 @"1988-09-08 12:30:12" 或者 @"1988年09月08日"..都会返回UI上展示的 1988年09月08日
+(NSDate *)getDateFromYYMMDD:(NSString *)time{
if (time == nil || [time isEqualToString:@""]) {
return nil;
}
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* inputDate = [inputFormatter dateFromString:time];
if (inputDate == nil) {
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy年MM月dd日"];
inputDate = [inputFormatter dateFromString:time];
}
if (inputDate == nil) {
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
inputDate = [inputFormatter dateFromString:time];
}
return inputDate;
}
+(NSString *)getBirthdayFromYYMMDD:(NSString *)time{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* inputDate = [inputFormatter dateFromString:time];
if (inputDate == nil) {
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
inputDate = [inputFormatter dateFromString:time];
}
if (inputDate == nil) {
[inputFormatter setDateFormat:@"yyyy年MM月dd日"];
inputDate = [inputFormatter dateFromString:time];
}
if (inputDate == nil) {
return @"";
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY年MM月dd日"];
NSString *dateString = [dateFormatter stringFromDate:inputDate];
return dateString;
}
+(NSDate *)getDateFromStr:(NSString *)timeStr{
if([HBUtils isEmpty:timeStr]){
return nil;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
return [dateFormatter dateFromString:timeStr];
}
+ (NSDateComponents *)getDateComponents:(NSString *)time{
return [HBUtils getDateComponentsWithDate:[self getDateFromStr:time]];
}
+ (NSDateComponents *)getDateComponentsWithDate:(NSDate *)date{
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 通过已定义的日历对象,获取某个时间点的NSDateComponents表示,并设置需要表示哪些信息(NSYearCalendarUnit, NSMonthCalendarUnit, NSDayCalendarUnit等)
NSDateComponents *dateComponents = [greCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekOfMonth fromDate:date];
return dateComponents;
}
+(long long)longlongFromDate:(NSDate *)date{
return [date timeIntervalSince1970]*1000;
}
+(NSDate *)getDateFromLongLong:(long long)time{
return [NSDate dateWithTimeIntervalSince1970:time/1000];
}
服务器返回毫秒long型数据时,
//以毫秒为整数值的时间戳转换
//时间戳转化为时间NSDate
+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
// 格式化时间
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
// 毫秒值转化为秒
//+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
NSString* dateString = [formatter stringFromDate:date];
return dateString;
}
关于时区问题
//将本地日期字符串转为UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定输入输出格式
- (NSString *)getUTCFormateLocalDate:(NSString *)localDate
{
// NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// //输入格式
// [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
// NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
// [dateFormatter setTimeZone:timeZone];
// //输出格式
// [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
// NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
// return dateString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
//输出格式
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
return dateString;
}
//将UTC日期字符串转为本地时间字符串
//输入的UTC日期格式2013-08-03T04:53:51+0000
- (NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate
{
// NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// //输入格式
// [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
// NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
// [dateFormatter setTimeZone:localTimeZone];
// NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
// //输出格式
// [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
// return dateString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
//输入格式
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
//输出格式
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localTimeZone];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
return dateString;
}