Recommended up to 2 years.
Breast milk is the natural first food for babies, it provides all the energy and nutrients that the infant needs for the first months of life, and it continues to provide up to half or more of a child's nutritional needs during the second half of the first year, and up to one-third during the second year of life.
World Health Organization, Breastfeeding, 2018.
As the baby grows heavier, the mother's milk becomes insufficient. At year 2 the mother can only provide one third what the baby needs of nutrients, so then it is better to provide other foods.
[Quran 2.233] Mothers may nurse their infants for two whole years, for those who desire to complete the nursing-period...
Breastfeeding is limited to two years in the Quran.
How could an illiterate man who lived 1400 years ago have determined how long to breastfeed?