Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given year & month, how to check a particular day exists in that month

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.

like image 572
Rameswar Prasad Avatar asked Jul 17 '17 07:07

Rameswar Prasad


People also ask

What does the given year mean?

Related Definitions Given Year means the applicable fiscal year for which the Adjusted Weighted Votes are calculated.

What means year to year?

year-to-year (comparative more year-to-year, superlative most year-to-year) Being compared with those of a previous year; interannual.

What does N year mean?

Related Definitions Year n means the delivery year for RA Capacity. For example, if the RA Capacity delivery year is 2023 (Year n), then Year n-3 (2020) means the year that is three years prior to the RA Capacity delivery year 2023 (Year n). In this example Year n-2 is 2021 and Year n-1 is 2022.


1 Answers

DateComponents has an isValidDate property, which you can use for that purpose:

var components = DateComponents()
components.month = 2
components.year = 2016
components.day = 30
components.calendar = Calendar.current

print(components.isValidDate) // false
like image 100
Martin R Avatar answered Nov 14 '22 22:11

Martin R