Your code is working perfectly fine. If you would run your code in a Playground, you could see the actual Date
objects rather than there String representations. Even if you don't specify a DateFormatter
, under the hood, when doing print(Date())
, Swift uses a DateFormatter
to print Date
objects and hence that value will be a relative time instead of the absolute point in time provided by the Date
object.
In a Playground you can see that the actual startOfWeek
is "Sep 25, 2017 at 12:00 AM"
, while the endOfWeek
is "Oct 1, 2017 at 12:00 AM"
, which are the correct values for the current week.
Don't use print(Date())
for checking the correctness of Date
objects, since as you can see, it will yield unexpected results due to the underlying DateFormatter
s used.