It is recommended to complete two (lunar) years.
وَالْوَالِدَاتُ يُرْضِعْنَ أَوْلَادَهُنَّ حَوْلَيْنِ كَامِلَيْنِ لِمَنْ أَرَادَ أَنْ يُتِمَّ الرَّضَاعَةَ
‘And Mothers shall suckle their children for two complete years; for those who wish to complete the suckling (period).’ (Surah al-Baqarah, 233).
In the Hanafi Madhab it is an obligation to breastfeed the child as long as he requires it (up to two years). If the child no longer requires it before two years are complete, then it is recommended to complete two years. It is permitted to go beyond two years, but only up to two and a half years. It is not permitted to breastfeed beyond this point. As clarified by Imam al-Hasakafi and Imam Ibn Abideen:
(وَلَمْ يُبَحْ الْإِرْضَاعُ بَعْدَ مَوْتِهِ) لِأَنَّهُ جَزْءُ آدَمِيٍّ وَالِانْتِفَاعُ بِهِ لِغَيْرِ ضَرُورَةٍ حَرَامٌ عَلَى الصَّحِيحِ
أَنَّهُ وَاجِبٌ إلَى الِاسْتِغْنَاءِ، وَمُسْتَحَبٌّ إلَى حَوْلَيْنِ، وَجَائِزٌ إلَى حَوْلَيْنِ وَنِصْفٍ
Breastfeeding is not permitted beyond its time limit. Because it is a part of the human being, and using it is without a necessity is Haram according to the sound opinion.
Indeed it (breastfeeding) is Waajib as long as the child requires it. It is recommended to continue until two years and permitted up to two and a half years. (Radd al-Muhtaar).
And Allah (Most High) Knows best.
– Answered by Shaykh Noorud-deen (23.03.2021)
See also (video):