gpt4 book ai didi

ios - 两个日期之间的时间差导致错误或未知值

转载 作者:行者123 更新时间:2023-11-29 02:58:34 30 4
gpt4 key购买 nike

我想找出两个日期之间的时差。但我得到的值为 -0.0000nan-357683564。下面是我查找时差的代码。我在这个计算中做错了吗?

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeStyle = NSDateFormatterNoStyle;
formatter.dateFormat = @"YYYY-MM-dd HH:MM:SS ±HHMM";//MM/dd/yyyy
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale:usLocale];
NSDate *intialTime= [formatter dateFromString:[NSString stringWithFormat:@"%@",[dateArray objectAtIndex:i]]];//[formatter dateFromString:@"12/11/2005"];
NSDate *presentDate = [NSDate date];
NSTimeInterval timeInterval = [presentDate timeIntervalSinceDate:intialTime];
int seconds = (int)ceil(timeInterval);
int mins = seconds / 60;
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = timeInterval / secondsInAnHour;
NSLog(@"Time interval in Mins:%d -- %.4f -- %d",mins,timeInterval,hoursBetweenDates);

在日志中:

以分钟为单位的时间间隔:-35791394 -- nan -- -2147483648

intialTime - (null) and presentDate - 2014-05-09 10:30:15 +0000

最佳答案

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

NSDate *dateToday = [NSDate date];

NSString *date = @"09-05-14"; // Put your initial date here...

[df setDateFormat:@"HH:mm:ss"];
NSString *todayString = [df stringFromDate:[NSDate date]];
NSString *todaysDateTime = [date stringByAppendingString:todayString];

[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *initialDate = [df dateFromString:todaysDateTime];

NSTimeInterval ti = [initialDate timeIntervalSinceDate:dateToday];

关于ios - 两个日期之间的时间差导致错误或未知值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23562529/

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