NSDate和NSString
+(NSDate*) convertDateFromString:(NSString*)uiDate { NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ; [formatter setDateFormat:@"yyyy年MM月dd日"]; NSDate *date=[formatter dateFromString:uiDate]; return date; } 输入的日期字符串形如:@"2012-07-30 13:08:08" - (NSDate *)dateFromString:(NSString *)dateString{ NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSDate *destDate= [dateFormatter dateFromString:dateString]; [dateFormatter release]; return destDate; } - (NSString *)stringFromDate:(NSDate *)date{ NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; //zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"]; NSString *destDateString = [dateFormatter stringFromDate:date]; [dateFormatter release]; return destDateString; } //从起始日期经过一定天数后/前(day为负数)的日期 - (NSString *)dateStringFromDate:(NSDate *)date afterDay:(int)day { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init]; //这边填入需要增加的天数 [componentsToAdd setDay:day]; NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate *fromDate=date; NSDate *dateAfterDay = [calendar dateByAddingComponents:componentsToAdd toDate:fromDate options:0]; [componentsToAdd release]; NSString *dateStr = [dateFormatter stringFromDate:dateAfterDay]; [dateFormatter release]; return dateStr; }