gpt4 book ai didi

ios - 在 objective-c 的选定日期中使用 dateWithTimeInterval 添加 3 秒后获取无效时间

转载 作者:行者123 更新时间:2023-11-28 22:06:52 25 4
gpt4 key购买 nike

我想动态添加 3 秒。

“20-05-2014 3-08-58” 是我选择的日期。现在我想及时添加 3 秒,比如新日期是 “20-05-2014 3-09-01”。我为此使用了以下逻辑,但它不起作用。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [格式化程序 setDateFormat:@"dd-MM-yy HH:mm:ss"];
NSDate *date = [格式化程序 dateFromString:strDateTime]
NSDate *newDate = [NSDate dateWithTimeInterval:3.0 sinceDate:date];

使用上述逻辑,我得到了无效的 "20-05-2014 07-29-01" 之类的新日期。

那么如何解决这个无效时间问题。

提前致谢...!

最佳答案

你在 DateFormatter 中有几个错误:

查看更正的解决方案:

NSString *strDateTime = @"20-05-2014 3-08-58";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH-mm-ss"];
NSDate *date = [formatter dateFromString:strDateTime];
NSDate *newDate = [NSDate dateWithTimeInterval:3 sinceDate:date];

NSLog(@"cur:%@, next:%@",date, newDate);

输出:

cur:2014-05-19 21:38:58 +0000, next:2014-05-19 21:39:01 +0000

编辑:

因为您希望格式中的日期有 +3 秒,所以您需要再次将 NSDate 值转换为 NSString。

NSString *dateAfter3Sec = [formatter stringFromDate:newDate];

NSLog(@"%@",dateAfter3Sec);

它会给你 20-05-2014 03-09-01

关于ios - 在 objective-c 的选定日期中使用 dateWithTimeInterval 添加 3 秒后获取无效时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23756382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com