I am trying to check if a particular day exists in a month or not. However unable to find anything of help. To give an example I tried below code in Playground;
var components = DateComponents()
components.month = 2
components.year = 2016
components.day = 30
components.calendar = Calendar.current
components.date
This gives the answer;
"Mar 1, 2016, 12:00 AM"
So what it does is it tries to manipulate with TimeZones and moves the needle. Hence I'm unable to find a way to check if a particular day exists in a given month & year. Can anyone please guide.
Lets break this down to -
Then your problem boils down to check if the given day is within the range [1, numDaysInMonthYear).
The code for above is this:
Credits: How do I find the number of days in given month and year using swift
DateComponents
has anisValidDate
property, which you can use for that purpose:try this below method you can check the particular day exists in a month.
Call by
You can check by using
NSDateFormatter / DateFormatter
You can change the date formatter's dateFormate based on your input string.