I have a date picker that returns me a NSdate value. And I want to have a date value of seconds set to 0. I have the code to do it in objective c as
NSTimeInterval time = floor([date timeIntervalSinceReferenceDate] / 60.0) * 60.0;
return [NSDate dateWithTimeIntervalSinceReferenceDate:time];
where date
is the datepicker's date. So how to realise this in swift?
It is almost identical in Swift:
The same can be achieved with
NSCalendar
methods:and this has the great advantage that it can easily be adapted for larger time units like days, months, etc. which do not have a fixed length (e.g. a day can have 23, 24, or 25 hours in regions with daylight saving time).