我在 24 小时内有两个日期,比如 (dd-MMM-yyyy HH:mm(。我想在这两个日期之间得到一个区别。
我的代码:这只得到日期,我想要几天和剩余时间(以小时为单位(。(如 6 天 5 小时(
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);
NSDateComponentsFormatter
可以做到这一点
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
NSLog(@"%@", [formatter stringFromTimeInterval:secondsBetween]);
有关其他样式,请参阅NSDateComponentsFormatterUnitsStyle
。
Swift 3+
您可以使用日期组件,例如:
let date1 = Date()
let date2 = Date(timeIntervalSinceNow: 12345678)
let dateComponets = Calendar.current.dateComponents([.day, .hour], from: date1, to: date2)
然后,您可以看到天数的差异:
debugPrint(dateComponets.day, dateComponets.hour) // 142 (days), 22 (hours)
ObjC
相同的方法,但语法不同:
NSDate * date1 = [NSDate new];
NSDate * date2 = [NSDate dateWithTimeIntervalSinceNow:12345678];
NSDateComponents * dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitHour fromDate:date1 toDate:date2 options:0];
然后以天为单位的差异:
NSLog(@"%d, %d", dateComponents.day, dateComponents.hour); // 142 (days), 22 (hours)
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)
}
你可以这样
let eventDate = Date().getMonthAndYearBetween(from: startDate, to: endDate)