Search code examples
iosswiftxcodecalendardateformatter

Extract value of NSDate()


I am trying to get a number of the day in 14 days from today. I want to work with the number so it has to be an integer value.

But I don't find a solution for extracting the information I need. So my question is how to get the date in 14 days so I can work with it?

let daysToAdd = 14
let newDate = Calendar.current.date(byAdding: .day, value: daysToAdd, to: Date())

//This is where I want to get
print ("In 14 days it is \(day).\(month).\(year)

Solution

  • let newDate = Calendar.current.date(byAdding: .day, value: 14, to: Date())
    
    let calendar = Calendar.current
    print(calendar.component(.year, from: newDate!)) // Year
    print(calendar.component(.month, from: newDate!)) // Month
    print(calendar.component(.day, from: newDate!)) // Day