如果各月包含,则包括31天,同时计算Swift 3.0中两个日期之间的差异?

时间:2017-06-21 05:51:53

标签: ios swift nscalendar

我需要准确显示两个日期之间的差异。 这正是我在做的事情

func getDifferenceBetween(dateString1:String, dateString2:String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date1 = formatter.date(from: dateString1)
let date2 = formatter.date(from: dateString2)

let form = DateComponentsFormatter()
form.maximumUnitCount = 2
form.unitsStyle = .abbreviated
form.allowedUnits = [.year, .month, .day]
let difference = (form.string(from: date1!, to: date2!))?.capitalized

print("difference- \(difference)")
 return difference!
}

问题是它不考虑月份是否包含31天或者年份是否为闰年。

例如:

dateString1 = "2017-04-08 13:31:19" 
dateString2 = "2017-06-21 11:17:11" 

getting difference  = 2M 12D
should get difference = 2M 15D

2 个答案:

答案 0 :(得分:-1)

这是代码的一部分。它使用当前用户日历为您提供2个日期之间的日期和月份。

func getDifferenceBetween(dateString1:String, dateString2:String) -> String {

    let currentCalendar = Calendar.current

    // Format and check dates
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    guard let date1 = formatter.date(from: dateString1),
        let date2 = formatter.date(from: dateString2),
        let m1 = currentCalendar.dateComponents([.month], from: date1).month,
    let m2 = currentCalendar.dateComponents([.month], from: date2).month,
    let d1 = currentCalendar.dateComponents([.day], from: date1).day,
    let d2 = currentCalendar.dateComponents([.day], from: date2).day

    else { return "Invalid Dates"
    }

    let months = m2 - m1
    let days = d2 - d1


    let difference = "\(months)M, \(days)D"
    return difference
}


getDifferenceBetween(dateString1: "2017-04-08 13:31:19",
                     dateString2: "2017-06-21 11:17:11")

我不明白你为什么期望2M 15D。我希望它可以提供帮助。 备注使用Guard语句并尝试永远不要使用!在你的代码中。

答案 1 :(得分:-2)

可能它的代码对你更好吗?它根据每个日期的小时数来调整日期

func getDifferenceBetween(dateString1:String, dateString2:String) -> String {

    let currentCalendar = Calendar.current

    // Format and check dates
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    guard let date1 = formatter.date(from: dateString1),
        let date2 = formatter.date(from: dateString2),
        let m1 = currentCalendar.dateComponents([.month], from: date1).month,
    let m2 = currentCalendar.dateComponents([.month], from: date2).month,
    let d1 = currentCalendar.dateComponents([.day], from: date1).day,
    let d2 = currentCalendar.dateComponents([.day], from: date2).day

    else { return "Invalid Dates"
    }

    // Calc Days and Month betwwen two dates
    let months = m2 - m1
    var days = d2 - d1

    // Compare Hours
    if currentCalendar.dateComponents([.hour, .minute, .second],
                                      from: date1, to: date2).hashValue > 0 {
        days = days - 1
    }


    let difference = "\(months)M, \(days)D"
    return difference
}


getDifferenceBetween(dateString1: "2017-04-08 13:31:19",
                     dateString2: "2017-06-21 11:17:11")