问题
I have two dates in 24 hours formate like (dd-MMM-yyyy HH:mm). I want to get a difference between these two dates.
My Code : This is getting only dates, I want a number of days and remaining time in hours also. (like 6days 5 hours)
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);
回答1:
NSDateComponentsFormatter can do that
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
NSLog(@"%@", [formatter stringFromTimeInterval:secondsBetween]);
See NSDateComponentsFormatterUnitsStyle for other styles.
回答2:
Swift 3+
you can use date-components, like:
let date1 = Date()
let date2 = Date(timeIntervalSinceNow: 12345678)
let dateComponets = Calendar.current.dateComponents([.day, .hour], from: date1, to: date2)
then you can see the difference in days:
debugPrint(dateComponets.day, dateComponets.hour) // 142 (days), 22 (hours)
ObjC
same approach, but different syntax:
NSDate * date1 = [NSDate new];
NSDate * date2 = [NSDate dateWithTimeIntervalSinceNow:12345678];
NSDateComponents * dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitHour fromDate:date1 toDate:date2 options:0];
then the difference in days:
NSLog(@"%d, %d", dateComponents.day, dateComponents.hour); // 142 (days), 22 (hours)
回答3:
struct EventDate {
var monthYear: [String]?
var monthNumber: [String]?
var year: [String]?
var month: [String]?
init(monthYear: [String]?,monthNumber:[String]?,year:[String]?,month:[String]?) {
self.monthYear = monthYear
self.monthNumber = monthNumber
self.year = year
self.month = month
}
}
func getMonthAndYearBetween(from start: String, to end: String) -> EventDate {
var monthYear: [String] = []
var monthNumber: [String] = []
var years: [String] = []
var months: [String] = []
let format = DateFormatter()
format.dateFormat = "dd/MM/yyyy"
guard let startDate = format.date(from: start),
let endDate = format.date(from: end) else {
return EventDate(monthYear: [], monthNumber: [], year: [], month: [])
}
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(Set([.month]), from: startDate, to: endDate)
var allDates: [String] = []
let dateRangeFormatter = DateFormatter()
dateRangeFormatter.dateFormat = "MMM yyyy"
for i in 0 ... (components.month! + 1) {
guard let date = calendar.date(byAdding: .month, value: i, to: startDate) else {
continue
}
let formattedDate = dateRangeFormatter.string(from: date)
allDates += [formattedDate]
let df = DateFormatter()
df.dateFormat = "MMM yyyy"
let monyr = df.string(from: date)
monthYear.append(monyr)
df.dateFormat = "MM"
let monNO = df.string(from: date)
monthNumber.append(monNO)
df.dateFormat = "yyyy"
let yr = df.string(from: date)
years.append(yr)
df.dateFormat = "MMM"
let mon = df.string(from: date)
months.append(mon)
}
return EventDate(monthYear: monthYear, monthNumber: monthNumber, year: years, month: months)
}
you can get like this
let eventDate = Date().getMonthAndYearBetween(from: startDate, to: endDate)
来源:https://stackoverflow.com/questions/59069228/get-difference-between-two-dates