I'm trying to set the seconds of a date object to 0 with the following:
if let date = value{
var calendar = Calendar.current
if let mdate = calendar.date(bySetting: .second, value: 0, of: date) {
...
}
}
However, this isn't just setting the seconds to 0, it's also adding 1 to the minutes, and I cannot understand why.
As you can see in this screenshot - I have a date object with a time value of "15:00:10", but this becomes "15:01:00" when I set the seconds to 0.
This does not seem intuitive. Do I need to subtract a minute each time? Can I be sure that this behavior is consistent?
That is odd, and I'm not quite sure why it increments the minute.
You can get the result you are after by fetching the year/month/day/hour/minute component values from your date and then using those to create another date:
let value: Date? = Date()
if let date = value{
var calendar = Calendar.current
print(date.description(with: .current))
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)
if let mdate = calendar.date(from: components) {
print(mdate.description(with: .current))
}
}