Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 1970年以来使用时间间隔的数学问题_Iphone_Ios_Objective C - Fatal编程技术网

Iphone 1970年以来使用时间间隔的数学问题

Iphone 1970年以来使用时间间隔的数学问题,iphone,ios,objective-c,Iphone,Ios,Objective C,我试图找出我们现在的时间已经过去了多少毫秒。我找不到以毫秒为单位返回时间的方法忽略日期,所以我想我可以根据timeIntervalSince 1970方法返回的值来计算它 我这样做: NSLog(@"%f", [[NSDate date] timeIntervalSince1970]); 2013-05-21 16:29:09.453 TestApp[13951:c07] 1369171749.453490 现在我的假设是,由于一天中有86400秒,我可以将这个值除以86400,得到自1970

我试图找出我们现在的时间已经过去了多少毫秒。我找不到以毫秒为单位返回时间的方法忽略日期,所以我想我可以根据timeIntervalSince 1970方法返回的值来计算它

我这样做:

NSLog(@"%f", [[NSDate date] timeIntervalSince1970]);
2013-05-21 16:29:09.453 TestApp[13951:c07] 1369171749.453490
现在我的假设是,由于一天中有
86400秒
,我可以将这个值除以
86400
,得到自1970年以来已经过去了多少天。这样做会给我15846.8952483天的时间。现在,如果我的假设成立的话,我今天的收入是89.52483%。所以多个
24小时乘以86.52659%
会给我一个
21.48592小时
或大约
09:29 PM
的当前时间。正如您可以从我的
NSLog
中看到的,这距离实时时间大约有5个小时,但我相信返回的时间间隔是
GMT
,因此这将比我的时区提前5个小时

所以我想,好吧,见鬼,我会继续下去,看看会发生什么

我通过以下操作删除小数点:

float timeSince1970 = [[NSDate date] timeIntervalSince1970]/86400.0;
timeSince1970 = timeSince1970 - (int)timeSince1970
然后计算今天到目前为止发生的毫秒数:

int timeNow = timeSince1970 * 86400000;
NSLog(@"%i", timeNow);
2013-05-21 16:33:37.793 TestApp[14009:c07] 77625000
然后我将毫秒(看起来仍然合适)转换为
NSDate

NSString *timeString = [NSString stringWithFormat:@"%d", timeNow];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"A"]
NSDate *dateNow = [dateFormatter dateFromString:timeString];
NSLog(@"%@", dateNow);
2013-05-21 16:29:09.455 TestApp[13951:c07] 2000-01-02 03:29:00 +0000
这就是我的问题。它不是返回附加了一些小时和分钟的
2000-01-01
日期,而是返回
2000-01-02
日期。为什么

编辑

我通过“删除”我在上面提到的额外的5个小时来实现它:

int timeNow = (timeSince1970 * 86400000) - (5 * 60 * 60 * 1000);
我不明白为什么这是必要的。如果有人能解释,我将不胜感激

编辑2

也许我应该问一个更基本的问题,关于如何完成我试图完成的任务。我关心时间(例如,下午4点很重要,但我不太关心日期)。我一直将这些存储在由以下人员创建的NSDate中:

[dateFormatter setDateFormat:@"hh:mm a"];
[dateFormatter dateFromString@"04:00 PM"];
这一切似乎进展顺利。现在,我想将当前时间与保存的时间进行比较,找出是取消发送还是取消发送,并做出相应的响应。有没有更好的方法来实现这一点

它将返回2000-01-02日期。为什么

因为您的
dateFormatter
使用当前系统区域设置的时区

如果你插入

dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
。。。您的日期格式化程序将正确解释字符串。但为什么不直接创建日期:

NSDate *dateNow = [NSDate dateWithTimeIntervalSinceReferenceDate:timeNow];

您需要使用
NSCalendar
根据立即生成
NSDateComponents
,然后将开始时间、分钟和秒全数设置为0。这将给你今天的开始。然后,您可以使用
NSDate
-timeIntervalSinceNow
方法获取从现在到开始日期之间经过的时间

NSCalendar *cal = [NSCalendar currentCalendar];

NSDate *now = [NSDate date];

// BUILD UP NSDate OBJECT FOR THE BEGINNING OF TODAY
NSDateComponents *comps = [cal components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: now];
comps.hour = 0;
comps.minute = 0;
comps.second = 0;

// USE CALENDAR TO GENERATE NEW DATE FROM COMPONENTS
NSDate *startOfToday = [cal dateFromComponents: comps];

// YOUR ELAPSED TIME
NSLog(@"%f", [startOfToday timeIntervalSinceNow]);
编辑1 如果您只是想比较一些NSDATE对象,您可以看到从那时到现在的时间间隔是否为负值。如果是这样,那么这个日期已经过去了

NSDate *saveDate = [modelObject lastSaveDate];
NSTimeInterval difference = [saveDate timeIntervalSinceNow];
BOOL firstDateIsInPast = difference < 0;
if (firstDateIsInPast) {
    NSLog(@"Save date is in the past");
}

你的问题中有一部分说,你想计算“我们现在的时间已经过去了多少毫秒”,然后“下午4点很重要,但我不太关心日期”,这就不需要回答了

这是因为“今天”可能发生了时间变化,它改变了从午夜开始的毫秒数(例如,通过增加或减少一小时,或在一年结束时增加或减少闰秒,等等),如果你没有日期,你就无法准确地确定毫秒数

现在,为了解决您编辑的问题:如果我们假设今天的日期,那么您需要使用存储的时间,并将其与今天的日期相结合,以获得“特定时间点”,您可以将其与当前日期和时间进行比较:

NSString *storedTime = @"04:00 PM";

// Use your current calendar 
NSCalendar *cal = [NSCalendar currentCalendar];

// Create a date from the stored time
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"hh:mm a"];
NSDate *storedDate = [dateFormatter dateFromString:storedTime];

// Break it up into its components (ie hours and minutes)
NSDateComponents *storedDateComps = [cal components:NSHourCalendarUnit | NSMinuteCalendarUnit 
                                           fromDate:storedDate];

// Now we get the current date/time:
NSDate *currentDateAndTime = [NSDate date];

// Break it up into its components (the date portions)
NSDateComponents *todayComps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                      fromDate:currentDateAndTime];

// Combine with your stored time
todayComps.hour = storedDateComps.hour;
todayComps.minute = storedDateComps.minute;

// Create a date from the comps.
// This will give us today's date, with the time that was stored
NSDate *currentDateWithStoredTime = [cal dateFromComponents:todayComps];

// Now, we have the current date and the stored value as a date, so it is simply a matter of comparing them:
NSComparisonResult result = [currentDateAndTime compare:currentDateWithStoredTime];

为什么不为“今天”上午12:00创建一个日期,然后从中减去当前时间?再想一想,我不知道这是否有效。我在我的应用程序中比较“时间”,但如果没有附加的日期组件,似乎没有办法做到这一点。如果您只提供小时,则所有内容似乎都默认为2000-01-01。您可以使用[NSDate dateWithString:…有关定义日期的帮助,请参阅NSDate文档抱歉,我会提供一些代码,但我必须运行-祝您好运:)我对该代码很熟悉。我将尝试一下。原始问题询问如何获取当前日期经过的毫秒数。这正确地解决了原始问题。将时间间隔乘以1000获得毫秒。无需将date components对象的小时、分钟和秒数设置为零。此外,
NSTimeInterval
是一个
双精度
;使用
%lf
打印它。您的方法确实回答了最初的问题,所以谢谢。我对它进行了投票,但我将接受Inafziger的答案,因为它是正确的为我提供了前进的最佳策略。我尝试了这个方法,它离应该的位置更近了,但现在离它应该的位置还有一个小时(我相信是因为夏令时)。我真的很喜欢这个方法。它似乎最有意义,我正在努力实施它,并会让你知道它的进展。
NSString *storedTime = @"04:00 PM";

// Use your current calendar 
NSCalendar *cal = [NSCalendar currentCalendar];

// Create a date from the stored time
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"hh:mm a"];
NSDate *storedDate = [dateFormatter dateFromString:storedTime];

// Break it up into its components (ie hours and minutes)
NSDateComponents *storedDateComps = [cal components:NSHourCalendarUnit | NSMinuteCalendarUnit 
                                           fromDate:storedDate];

// Now we get the current date/time:
NSDate *currentDateAndTime = [NSDate date];

// Break it up into its components (the date portions)
NSDateComponents *todayComps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                      fromDate:currentDateAndTime];

// Combine with your stored time
todayComps.hour = storedDateComps.hour;
todayComps.minute = storedDateComps.minute;

// Create a date from the comps.
// This will give us today's date, with the time that was stored
NSDate *currentDateWithStoredTime = [cal dateFromComponents:todayComps];

// Now, we have the current date and the stored value as a date, so it is simply a matter of comparing them:
NSComparisonResult result = [currentDateAndTime compare:currentDateWithStoredTime];