如何在添加30天之后修复日期格式

问题描述 投票:0回答:1

我将日期和时间转换为不同的字符串,并使用日期格式化器以日期格式将它们组合在一起。在此之后,我将在结果日期添加30天。现在我想再次使用dateFormatter但是无论我做什么都会导致nil(即使它不是nil)。我究竟做错了什么?

 //GET TIME AND DATE FROM TWO STRINGS
    var timeStarting = "10:50"
    var dateStarting = "03/10/2016"
    let date = Date()
    let timeStamp = NSDate().timeIntervalSince1970
    let dateFormater = DateFormatter()
    dateFormater.dateStyle = .short
    dateFormater.timeStyle = .none
    dateFormater.dateFormat = "dd/MM/yyyy HH:mm"
    var timeAndDate = "\(dateStarting) \(timeStarting)"
    let endingDate : Date = dateFormater.date(from: "\(timeAndDate)")!

    //ADD 30 DAYS TO ENDING DATE
    var numberOfDays = "30"
    let thirtyDaysBeforeToday = Calendar.current.date(byAdding: .day, value: Int(numberOfDays)!, to: endingDate)
    var stringFromThityDaysBefore : String = "\(thirtyDaysBeforeToday!)"

    //FORMAT AGAIN TO SHOW THE DATE IN AS (EX.: 13.10.2016)
    let dateFormater2 = DateFormatter()
    dateFormater2.isLenient = true
    dateFormater2.dateStyle = .short
    dateFormater2.timeStyle = .none
    dateFormater2.dateFormat = "dd.MM.yy"
    var newDates : Date = dateFormater2.date(from: "\(stringFromThityDaysBefore)")!

(结果出处:13.10.2016) - 结束日期后30天

swift swift3 nsdate nsdateformatter
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.