【问题标题】:Difference between dateWithTimeInterval:(NSTimeinterval) sincedate:(nsdate *) and (nsdate *) dateByAddingTimeInterval:(NSTimeInterval)dateWithTimeInterval:(NSTimeinterval) sincedate:(nsdate *) 和 (nsdate *) dateByAddingTimeInterval:(NSTimeInterval) 之间的差异
【发布时间】:2012-12-11 21:20:55
【问题描述】:

这两种说法有什么区别

NSDate *today = [NSDate date];
NSDate *tomarow = [today dateByAddingTimeInterval:60*60*24];
NSDate *nextday = [NSDate dateWithTimeInterval:60*60*24 sinceDate:today];

【问题讨论】:

  • @InderKumarRathore - 我得到的两个语句的值相同。它们之间有什么区别
  • @ray1th 检查我的答案!!

标签: objective-c nsdate nstimeinterval


【解决方案1】:

这两种方法的唯一区别是一种是类方法,另一种是实例方法。

以下代码 sn-p 演示了这两种方法的使用:

// Today's Date
NSDate *today = [NSDate new];

// Date With Class Method
NSDate *tomorrow1 = [NSDate dateWithTimeInterval:60*60*24 sinceDate:today];
NSLog(@"Date from class method: %@", tomorrow1);

// Date With Instance Method
NSDate *tomorrow2 = [today dateByAddingTimeInterval:60*60*24];
NSLog(@"Date from instance method: %@", tomorrow2);

上面的代码 sn-p 将给出如下输出:

上课日期:2012-12-27 09:35:15 +0000

来自实例方法的日期:2012-12-27 09:35:15 +0000

更多信息,请参阅NSDate

【讨论】:

  • 你能不能通过传递负值-60*60*24来做同样的事情
  • @InderKumarRathore 我们开始了:tomarrow 2012-12-25 09:46:50 +0000,第二天 2012-12-25 09:46:50 +0000!!怎么了?
  • 没什么问题,我现在在 Windows 上,所以无法检查。这就是我告诉你的原因。无论如何+1 f
  • @Dpk :对不起,我的错误......我正在睡觉......无论如何+1为你的答案:)
猜你喜欢
  • 2011-12-19
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
相关资源
最近更新 更多