I need to find months, years, days from given days. For example, 370 days, I need to convert to 1 year 0 month & 5 days. Days can be rounded off to the nearest month. For example, if days are less than 15, that should go to 0 month. If its more than 15 days, now that should go to 1 month
Asked
Active
Viewed 2,054 times
0
-
6Which calendar? Which year? In the Gregorian calendar, a year can have 365 or 366 days. In the Islamic calendar, it can be 354 or 355 days. And there are many more ... – Martin R Mar 23 '17 at 06:18
-
Do you need the calculation to be based on a specific date? For example, if you calculate 370 days from sometime in January, it will have to take February 29th into account. Basically, leap years = 366 days, non-leap years = 365 days. – Fahim Mar 23 '17 at 06:19
-
Check this link, may be help http://stackoverflow.com/questions/31590316/how-do-i-find-the-number-of-days-in-given-month-and-year-using-swift – Mahfuz Shishir Mar 23 '17 at 06:21
-
Even in the Gregorian calendar, 40 days can be 1 month and 9, 10, 11 or 12 days. – As you see, without more information, your problem is unclear. – Martin R Mar 23 '17 at 06:25
-
@MartinR I am looking for answer in Gregorian Calendar – venky Mar 23 '17 at 06:26
-
Your task is still unclear to me, you did not address the above questions. Some *concrete* examples how different numbers of days in a month/year should be handled would be helpful. – Martin R Mar 23 '17 at 06:35
-
number of days in each month can be diff, so also need a start date – Tj3n Mar 23 '17 at 07:14
-
Ok. Let me put it this way. If I said "how many months is 31 days?" Then what would the answer be? Well 31 days from Jan 1st is Jan 31st. So only 1 month. But what if it was from Jan 15th? Well 31 days after that is the middle of Feb so that would be 2 months. Ok... what about from Jan 31st? 31 days after Jan 31st is somewhere close to the beginning of March so that's 3 months. None of these are wrong answers but they are all different. You cant just arbitrarily say a number of days. Calendars don't work that way. – Fogmeister Mar 23 '17 at 08:22
2 Answers
4
Assuming you know starting and/or ending date, and not just distance between them, you can use this:
dateComponents(_:from:to:)
Returns the difference between two dates.Declaration
func dateComponents(_components: Set<Calendar.Component>, from start: Date, to end: Date) -> DateComponents
Parameters
components
Which components to compare.
start
The starting date.
end
The ending date.
Example:
let calendar = Calendar(identifier: <whatever calendar you need>)
let components: DateComponents = calendar.dateComponents([.calendar, .year, .month, .day], from: startDate, to: endDate)
And then from DateComponents
you get your years
, months
and days
and what not.

user28434'mstep
- 6,290
- 2
- 20
- 35
1
func numberOfDaysInCurrentYear() -> Int {
let calendar = Calendar.init(identifier: .gregorian)
let interval = calendar.dateInterval(of: .year, for: Date.init())!
let days = calendar.dateComponents([.day], from: interval.start, to: interval.end).day!
return days
}

Hemang
- 26,840
- 19
- 119
- 186