与将字符串转换为日期的日期比较

时间:2011-10-31 05:51:00

标签: iphone objective-c ios ios4 iphone-sdk-3.0

如何随时间改变字符串 我正在拧代码,但没有运气

NSString *comingdate = [sParams valueForKey:@"showdate"];
NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
[dateformate setDateFormat:@"yyyy-MM-dd"];
NSDate *todate = [dateformate dateFromString:comingdate];
NSLog(@"%@", todate);
来自showdate的

/ *我的日期是yyyy-MM-dd,但是我很想把这个字符串转换成日期与时间  任何人都可以提供线索或明确的代码

1 个答案:

答案 0 :(得分:2)

NSDate *firstDate=[[NSDate alloc]init];
    NSString *comingdate = [sParams valueForKey:@"showdate"]; // yyyy-MM-dd
    NSString *time = @"01:37:42"; // HH:mm:ss
    comingdate = [comingdate stringByAppendingFormat:@" %@", time];
    NSDateFormatter *dateformate = [[NSDateFormatter alloc] init];
    [dateformate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *secondDate = [dateformate dateFromString:comingdate];
    NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
    NSLog(@"%f",timeDifference);