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

标签 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 但我想将该字符串转换为带时间的日期 任何人都可以提供线索或使用清晰的代码

最佳答案

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);

关于iphone - 将字符串转换为日期的日期比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7950359/

相关文章:

objective-c - iPhone上远程备份用户数据

objective-c - 将鼠标限制在 Mac 上的一个显示器上(可能使用 Cocoa)

ios - 需要一种反向 CATransform3d

ios:如何使用 StoryBoards 从 ViewController 打开 Tab Bar Controller

IOS如何制作这样的可折叠菜单?

iphone - 声音引擎泄漏……预加载是一种不好的做法吗?

ios - 为什么 NSDateFormatter 在 iOS 7 上返回 nil

ios - 在 Objective-c 问题中访问委托(delegate)静态方法

android - Firebase 性能监控 React Native 集成

iphone 开发限制