对日期格式的处理

// 正则显示时间格式一天内一周内更早

+ (NSString*) compareCurrentTime:(NSString*)str

{

NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];

[dateFormattersetDateFormat:@"yyyyMMddHHmmss"];

NSDate*timeDate = [dateFormatterdateFromString:str];

NSDate*currentDate = [NSDatedate];

NSTimeIntervaltimeInterval = [currentDatetimeIntervalSinceDate:timeDate];

longtemp =0;

NSString*result;

// 根据当前时间获得本周还有几天

inta= [ttTableViewCellweekDayStr:str];

NSLog(@"----------+%d",a );

if((temp = timeInterval/60) <60){

if([strisEqualToString:@"0"]) {

returnnil;

}

NSString*s1= [strsubstringWithRange:NSMakeRange(8,4)];

NSMutableString* str1=[[NSMutableStringalloc]initWithString:s1];//存在堆区,可变字符串

NSLog(@"str1:%@",str1);

[str1insertString:@":"atIndex:2];//把一个字符串插入另一个字符串中的某一个位置

NSLog(@"str1:%@",str1);

result = [NSStringstringWithFormat:@"%@",str1];

}

// 如果是当天内 截取字符串 显示分钟小时

elseif((temp = temp/60) <24){

NSString*s1= [strsubstringWithRange:NSMakeRange(8,4)];

NSMutableString* str1=[[NSMutableStringalloc]initWithString:s1];//存在堆区,可变字符串

NSLog(@"str1:%@",str1);

[str1insertString:@":"atIndex:2];//把一个字符串插入另一个字符串中的某一个位置

NSLog(@"str1:%@",str1);

result = [NSStringstringWithFormat:@"%@",str1];

}

// 把截取出来的天数判断为周几

elseif((temp = temp/24)

NSDate*birthdayDate = [dateFormatterdateFromString:str];

result =[NSStringstringWithFormat:@"%@",[ttTableViewCellweekdayStringFromDate:birthdayDate]] ;

}

// 显示年月日截取字符串加.

else{

NSString*s1= [strsubstringWithRange:NSMakeRange(0,8)];

NSMutableString* str1=[[NSMutableStringalloc]initWithString:s1];//存在堆区,可变字符串

NSLog(@"str1:%@",str1);

[str1insertString:@"."atIndex:4];//把一个字符串插入另一个字符串中的某一个位置

[str1insertString:@"."atIndex:7];//把一个字符串插入另一个字符串中的某一个位置

NSLog(@"str1:%@",str1);

result = [NSStringstringWithFormat:@"%@",str1];

}

returnresult;

}

// 正则判断本周还剩余几天

+(int)weekDayStr:(NSString*)format

{

NSString*weekDayStr =nil;

NSDateComponents*comps = [[NSDateComponentsalloc]init];

NSString*str = [selfdescription];

if(str.length>=10) {

NSString*nowString = [strsubstringToIndex:10];

NSArray*array = [nowStringcomponentsSeparatedByString:@"-"];

if(array.count==0) {

array = [nowStringcomponentsSeparatedByString:@"/"];

}

if(array.count>=3) {

intyear =[[arrayobjectAtIndex:0]integerValue];

intmonth =[[arrayobjectAtIndex:1]integerValue];

intday =[[arrayobjectAtIndex:2]integerValue];

[compssetYear:year];

[compssetMonth:month];

[compssetDay:day];

}

}

NSCalendar*gregorian = [[NSCalendaralloc]

initWithCalendarIdentifier:NSGregorianCalendar];

NSDate*_date = [gregoriandateFromComponents:comps];

NSDateComponents*weekdayComponents = [gregoriancomponents:NSWeekdayCalendarUnitfromDate:_date];

intweek =[weekdayComponentsweekday];

week ++;

switch(week) {

case7:

weekDayStr =@"0";

break;

case1:

weekDayStr =@"1";

break;

case2:

weekDayStr =@"2";

break;

case3:

weekDayStr =@"3";

break;

case4:

weekDayStr =@"4";

break;

case5:

weekDayStr =@"5";

break;

case6:

weekDayStr =@"6";

break;

default:

weekDayStr =@"0";

break;

}

return[weekDayStrintValue];

}

// 正则判断为周几

+(NSString*)weekdayStringFromDate:(NSDate*)inputDate {

NSArray*weekdays = [NSArrayarrayWithObjects: [NSNullnull],@"星期天",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",nil];

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

NSTimeZone*timeZone = [[NSTimeZonealloc]initWithName:@"Asia/Shanghai"];

[calendarsetTimeZone: timeZone];

NSCalendarUnitcalendarUnit =NSCalendarUnitWeekday;

NSDateComponents*theComponents = [calendarcomponents:calendarUnitfromDate:inputDate];

return[weekdaysobjectAtIndex:theComponents.weekday];

}

你可能感兴趣的:(对日期格式的处理)