Answers:
NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"];
NSDate *date2 = [NSDate dateWithString:@"2010-02-03 00:00:00 +0000"];
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);
请记住,NSDate
对象代表确切的时刻,它们没有任何关联的时区信息。当您使用例如一个字符串转换为日期NSDateFormatter
时,NSDateFormatter
从配置的时区的时间转换。因此,两个NSDate
对象之间的秒数始终是时区不可知的。
此外,本文档还指定可可的时间实现方式不包含leap秒,因此,如果您需要这种准确性,则需要滚动自己的实现方式。
cocoa
,而不是cocoa-touch
那么这个答案只适用于Mac OS X.
您可能要使用以下内容:
NSDateComponents *components;
NSInteger days;
components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit
fromDate: startDate toDate: endDate options: 0];
days = [components day];
我相信这种方法可以解决诸如夏令时变化之类的日期。
NSDate
对象已经与时区无关。当使用例如解析日期时,时区信息将被删除NSDateFormatter
,以便NSDate
对象表示确切的时间点。因此,如果您已有startDate
和endDate
,则这两个对象之间的间隔为x秒。时区与它无关。
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // in seconds
在date1
和date2
在NSDate
。
另外,请注意以下定义NSTimeInterval
:
typedef double NSTimeInterval;
签出这个。它使用iOS日历来计算夏令时和leap年,您可以将字符串和条件更改为包括小时和天的分钟。
+(NSString*)remaningTime:(NSDate*)startDate endDate:(NSDate*)endDate
{
NSDateComponents *components;
NSInteger days;
NSInteger hour;
NSInteger minutes;
NSString *durationString;
components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: startDate toDate: endDate options: 0];
days = [components day];
hour = [components hour];
minutes = [components minute];
if(days>0)
{
if(days>1)
durationString=[NSString stringWithFormat:@"%d days",days];
else
durationString=[NSString stringWithFormat:@"%d day",days];
return durationString;
}
if(hour>0)
{
if(hour>1)
durationString=[NSString stringWithFormat:@"%d hours",hour];
else
durationString=[NSString stringWithFormat:@"%d hour",hour];
return durationString;
}
if(minutes>0)
{
if(minutes>1)
durationString = [NSString stringWithFormat:@"%d minutes",minutes];
else
durationString = [NSString stringWithFormat:@"%d minute",minutes];
return durationString;
}
return @"";
}
使用Swift 5和iOS 12,根据您的需要,您可以使用以下两种方法之一来查找两天之间的日期差异。
Calendar
的dateComponents(_:from:to:)
方法import Foundation
let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let dateComponents = calendar.dateComponents([Calendar.Component.day], from: startDate, to: endDate)
print(dateComponents) // prints: day: 1621 isLeapMonth: false
print(String(describing: dateComponents.day)) // prints: Optional(1621)
DateComponentsFormatter
的string(from:to:)
方法import Foundation
let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.allowedUnits = [NSCalendar.Unit.day]
let elapsedTime = formatter.string(from: startDate, to: endDate)
print(String(describing: elapsedTime)) // prints: Optional("1,621 days")
Swift 4
试试看,看一下(带字符串的日期范围):
// Start & End date string
let startingAt = "01/01/2018"
let endingAt = "08/03/2018"
// Sample date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
// start and end date object from string dates
var startDate = dateFormatter.date(from: startingAt) ?? Date()
let endDate = dateFormatter.date(from: endingAt) ?? Date()
// Actual operational logic
var dateRange: [String] = []
while startDate <= endDate {
let stringDate = dateFormatter.string(from: startDate)
startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date()
dateRange.append(stringDate)
}
print("Resulting Array - \(dateRange)")
迅捷3
var date1 = Date(string: "2010-01-01 00:00:00 +0000")
var date2 = Date(string: "2010-02-03 00:00:00 +0000")
var secondsBetween: TimeInterval = date2.timeIntervalSince(date1)
var numberOfDays: Int = secondsBetween / 86400
print(numberOfDays)
您可以通过以秒为单位转换日期并采用自1970年以来的时间间隔来找到差异,然后可以找到两个日期之间的差异。
要发现差异,您需要获取当前日期和将来的日期。在以下情况下,我以2天作为未来日期的示例。计算方式:
2 days
* 24 hours
* 60 minutes
* 60 seconds
。我们预计2天的秒数为172,800。
// Set the current and future date
let now = Date()
let nowPlus2Days = Date(timeInterval: 2*24*60*60, since: now)
// Get the number of seconds between these two dates
let secondsInterval = DateInterval(start: now, end: nowPlus2Days).duration
print(secondsInterval) // 172800.0