gpt4 book ai didi

iOS 时间戳计算

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

我想知道在 iOS 中是否有其他更好的时间戳计算,我可以知道你对这个计算的看法这个计算是否存在问题或错误。

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];

NSDate * startDate = [formatter dateFromString:dateString];
NSTimeInterval interval = [startDate timeIntervalSinceNow];

int totalSeconds = abs((int)interval);

int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
int days = totalSeconds / (60 * 60 * 24);
int weeks = totalSeconds / (60 * 60 * 24 * 7);
int months = totalSeconds / (60 * 60 * 24 * 7 * 4);
int years = totalSeconds / (60 * 60 * 24 * 7 * 4 * 12);

非常感谢您的回复。

最佳答案

NSDateComponents 正是您要找的。

关于iOS 时间戳计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17020066/

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