- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试计算两个日期之间的天数。这是我的做法:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int calendarFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDate *dateToCheck = (self.subscriptionEnd ?: self.trialEnd);
NSLog(@"dateToCheck: %@", dateToCheck);
NSLog(@"current date: %@", [self systemTimeZoneDate]);
NSDateComponents *components = [gregorian components:calendarFlags
fromDate:[self systemTimeZoneDate]
toDate:dateToCheck
options:0];
return [components day] >= 0 ?: 0;
在撰写本文时,NSLog 输出以下内容:
2014-09-05 22:56:20.054 tweepy[9635:60b] dateToCheck: 2014-10-05 08:02:51 PM +0000
2014-09-05 22:56:20.057 tweepy[9635:60b] current date: 2014-09-05 10:56:20 PM +0000
它返回一天的差异,因为 iOS 认为该天采用 YDM 格式。
我应该在某处指示日期格式吗?
以下是如何设置 self.subscriptionEnd
的代码:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 30;
self.subscriptionEnd = [gregorianCalendar dateByAddingComponents:dateComponents toDate:[self systemTimeZoneDate] options:0];
以下是如何设置 self.TrialEnd
的代码:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 2;
self.trialEnd = [gregorian dateByAddingComponents:dateComponents toDate:[self systemTimeZoneDate] options:0];
最佳答案
日期格式与它没有任何关系。正在发生的事情是 NSDateComponents
为您提供了您所要求的,即年、月和日的总差异。如果您只需要天数,则只需提供 NSDayCalendarUnit
。 docs说清楚:
Some operations can be ambiguous, and the behavior of the computation is calendar-specific, but generally larger components will be computed before smaller components; for example, in the Gregorian calendar a result might be 1 month and 5 days instead of, for example, 0 months and 35 days.
关于ios - 两个日期之间经过的天数总是相差一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25694676/
我使用此代码来测量我的 C 程序的计算时间: clock_t start = clock() ; do_some_work() ; clock_t end = clock() ; double ela
我正在尝试找到一个 SQL 查询,该查询将计算至少相差 30 分钟的不同开始时间的数量。 我有一些员工在一周内至少三个不同的时间开始工作时获得积分,其中开始时间与其他开始时间至少相差 30 分钟。 例
我正在尝试显示一个时间表,并且我想在一周内的每组比赛之后创建一个休息时间。例如,我想要一个 在第一周的前五场比赛之后。然后在第二周的五场比赛之后再次进行。我现在所拥有的包括前六场比赛,然后是之后的每五
float a=67107842,b=512; float c=a/b; printf("%lf\n",c); 为什么 c 是 131070.000000 而不是正确的值 131070.0039062
我有一个谓词 predicate = [NSPredicate predicateWithFormat:@"character.id IN %@", indexs]; 它生成以下 SQL: CoreD
我正在编写一个在 UI 中有一个 DatePicker 和一个 TimePicker 的应用程序。我需要获取用户设置的日期和时间并存储在服务器中。 例如用户选择“2015 年 11 月 13 日 13
我有一个带有 OffsetDateTime 的 JPA 实体类像这样的字段: @Entity class MyEntity(clock: Clock) { // ... val cre
这个问题已经有答案了: What does size of the memcmp return value mean? (2 个回答) 已关闭 3 年前。 所以我必须使用 C 重新创建 memcmp(
两个查询。第一个比第二个长 200 倍。为什么?PostgreSQL 10.1。 Metro 和 Sel - 同一张 table 上的 View 。 EXPLAIN ANALYZE SELECT *
我在 Google Play 中有一个应用,并在其上进行 Firebase 分析。我正在尝试跟踪广告来源。我不明白正确的下载次数在哪里,因为 Google Play Console 显示 150 个安
我正在使用 Python 进行核密度估计,并使用高斯混合模型对多维数据样本的可能性进行排序。每条数据都是一个角度,我不确定如何处理机器学习角度数据的周期性。 首先,我通过添加 360 来移除所有负角,
最近我遇到了一件非常奇怪的事情——一种方法在性能分析器下非常慢,没有明显的原因。它包含很少的 long 操作,但被调用得相当频繁 - 它的总体使用量约为总程序时间的 30-40%,而其他部分似乎“更重
请有人向我解释这种情况。 我有以下代码: Click the button to display the date and time as a string, using the ISO standa
如何在考虑时间复杂度的情况下找到从 1 到 20 亿(使用任何编程语言且不使用任何外部库)相差 6 的连续质数对的数量,例如 (23,29)? 尝试过埃拉托色尼筛法,但获得连续素数是一项挑战 使用了生
我正在尝试找到一种方法来过滤两个日期/时间字段的差异小于 90 分钟的记录。 例子: orders.created_at = 2015-08-09 20:30:20 table2.created_at
我在使用 EEPlus 库从 excel (.xlsx) 文件获取正确的日期字段值时遇到问题。 具体问题是在 excel 中我有例如1900.01.04,但在 C# 中我得到 1900.01.03。
我是一名优秀的程序员,十分优秀!