I generate 7 dates by adding days to current date. I add all the dates in an Array. Now when I pick any date, it pulls the correct date. Then I create components of that date. Now when I print the component, it show the date(day part) as next day's date. So, if choose 27-05-2016, and then get its components, and when I check, component.day, it gives me 28 instead of 27.
Here is my code to generate dates :
let cal = NSCalendar.currentCalendar()
// start with today
let selectedDate = cal.startOfDayForDate(NSDate())
for index in 1...7 {
dateArray.addObject(selectedDate.dateByAddingTimeInterval(Double(index)*24*60*60))
print(dateArray.objectAtIndex(index-1))
}
Here is the code where I choose a date from the above array and fetch its components :
if currentIndexPosition>=0 {
selectedDate = dateArray.objectAtIndex(currentIndexPosition) as? NSDate
}
else
{
selectedDate = dateArray.objectAtIndex(0) as? NSDate
}
let cal = NSCalendar.currentCalendar()
let components = cal.components([.Day , .Month, .Year, .Hour,.Minute ], fromDate: selectedDate!)
Now when I check the components the date is not the same that I chose from the array. Its one more than the selected date.
Please let me know , if I missed anything or you need any other info.
Thanks