|
Apologies for the questions but I have another. I just don't get dates in Swift. If anyone can give me guidance here it would really help. I need to calculate the first and last date of the "current month", and the first and last date of the "previous 11 months". So assuming March 1st is today I need to get something like this:
Anything you folks can advise on would be super. Thank you! |
|
You can get first and last days of the month like so:
and then format those dates as needed. |
|
Here is more eleborated example. Just add guard statements as needed
|
|
|
|
There is a simple way to get the start and end of the current month.
Since the interval assumes up to but not including the end date, the end date will be the start of the next month. Similarly, you can obtain the interval for the week with
The week interval observes the locale's start of week setting. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.