1,日期格式化
一般后台传会的时间是20180320之类的 此时如果需要展示不同的格式,就需要格式化数据方法如下根据后台传会的数据位数来分别展示不同的格式;
+ (NSString *)dateFormat:(NSString *)string
{
if (string) {
NSDateFormatter *format = [[NSDateFormatter alloc]init];
NSDateFormatter *format2 = [[NSDateFormatter alloc]init];
[format setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
if (string.length == 4) {
[format setDateFormat:@"HHmm"];
[format2 setDateFormat:@"HH:mm"];
}
else if(string.length == 8) {
[format setDateFormat:@"yyyyMMdd"];
[format2 setDateFormat:@"yyyy-MM-dd"];
}
else if (string.length == 12){
[format setDateFormat:@"yyyyMMddHHmm"];
[format2 setDateFormat:@"yyyy-MM-dd HH:mm"];
}
else if(string.length == 14) {
[format setDateFormat:@"yyyyMMddHHmmss"];
[format2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
}
else
{
return string;
}
NSDate *date = [format dateFromString:string];
return [format2 stringFromDate:date];
}
return nil;
}
2,时间差计算
[self timeWithBlock:^{//请求当前系统时间返回数据为时间戳
NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:[self.timesTamp longLongValue]/1000];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setLocale:[NSLocale currentLocale]];
[format setDateFormat:@"yyyyMMdd"];
NSString *nowStr = [format stringFromDate:nowDate];
#pragma mark 此处标记计算时间会相差8小时 但是不影响计算结果故此不予处理 在转换方法处理方法为设置时区就好了
// NSTimeZone *timeZone=[NSTimeZonetimeZoneWithName:@"UTC"];
// [formatter setTimeZone:timeZone];
NSDate *enddate = [self StringTODate:nowStr];
NSDate *beginDate = [self StringTODate:statusValue];
NSTimeInterval time = [enddate timeIntervalSinceDate:beginDate];
//开始时间和结束时间的中间相差的时间
int days;
days = ((int)time)/(3600*24); //一天是24小时*3600秒
NSString * dateValue = [NSString stringWithFormat:@"%i",days];
if ( dateValue.doubleValue >10) {
self.nextBtn.userInteractionEnabled = YES;
self.nextBtn.backgroundColor = DB_Blue;
}else{
self.nextBtn.userInteractionEnabled = NO;
self.nextBtn.backgroundColor = DB_GrayBlock;
}
}];
//字符串转日期
- (NSDate *)StringTODate:(NSString *)sender
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyyMMdd";
[dateFormatter setMonthSymbols:[NSArray arrayWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil]];
NSDate * ValueDate = [dateFormatter dateFromString:sender];
return ValueDate;
}
3,计算年龄
#pragma mark 根据身份证日期判断年龄
- (NSInteger)ageWithDateOfBirth:(NSString *)dateStr//,年龄检查
{
[self timeReq];//根据后台时间戳和身份证上的出生年月日来判断年龄。
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
//如果DateFormatter的时区设置为我国实行了夏令时的城市,19860504、19870412、19880410、19890416、19900415、19910414 这些日期转换成NSDate对象为nil
// 这个case的代码修改方式为:给dateFormatter显示设置时区偏移量
if ([dateStr isEqualToString:@"19860504"]||[dateStr isEqualToString:@"19870412"]||[dateStr isEqualToString:@"19880410"]||[dateStr isEqualToString:@"19890416"]||[dateStr isEqualToString:@"19900415"]||[dateStr isEqualToString:@"19910414"]) {
[format setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:3600*8]];
}
[format setDateFormat:@"yyyyMMdd"];
NSDate *date = [format dateFromString:dateStr];
USE_INFO.brithDay = date;
// 出生日期转换 年月日
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
NSInteger brithDateYear = [components1 year];
NSInteger brithDateDay = [components1 day];
NSInteger brithDateMonth = [components1 month];
// 获取系统当前 年月日
NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:[self.timesTamp longLongValue]/1000];
MyLog(@"nowDate:%@",nowDate);
NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:nowDate];
NSInteger currentDateYear = [components2 year];
NSInteger currentDateDay = [components2 day];
NSInteger currentDateMonth = [components2 month];
// 计算年龄
NSInteger iAge = currentDateYear - brithDateYear - 1;
if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
iAge++;
}
self.loanmodel.birthDate = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)brithDateYear,(long)brithDateMonth,(long)brithDateDay];
NSLog(@"\n====>出生年:%ld,月:%ld,日:%ld,\n====>现在年:%ld,月:%ld,日:%ld,\n====>年龄:%ld",(long)brithDateYear,(long)brithDateMonth,(long)brithDateDay,(long)currentDateYear,(long)currentDateMonth,(long)currentDateDay,(long)iAge);
return iAge;
}
4,设置营业时间
//使用时间戳转为日期通过日期转成时字符串,判断大小即可
[self timeWithBlock:^{
NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:[self.timesTamp longLongValue]/1000];
NSLog(@"nowDate:%@",nowDate);
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setLocale:[NSLocale currentLocale]];
[format setDateFormat:@"HHmmss"];
NSString *nowStr = [format stringFromDate:nowDate];
NSLog(@"nowStr = %@",nowStr);
NSInteger nowValue = [nowStr integerValue];
if (nowValue >= 73000 && nowValue < 193000) {
//在营业时间段的操作此时间段为早上七点半到晚上七点半
}else{
//不在时间段的弹窗提示
}otherTitle:nil otherBolck:nil];
}
}];