Almost any time of the year is a good time to visit Madrid with the family.
For children, Madrid has lot of things to see and do. Just be aware that the weather in Madrid can change quickly so carry some clothing options especially if you are traveling with young children who might not be comfortable with sudden cold or hot weather.
June to September is the best time
June to September is the best time for families to visit Madrid when it is sunny and dry. The weather is predictable and children can easily run around in comfortable clothes. But August can be a bit hot for toddlers so carry light clothing that covers their delicate skin but still keeps them cool.
Next best is in the fall
The fall months of October and November start getting cool and the weather is quite crisp for a family visit. If you decide to visit Madrid during this time expect sudden changes in weather from day to day or even on the same day as the season transitions from summer to winter.
Avoid winter months
Winters in Madrid can get cold and some days it might even snow, so November to February may not be a great idea to plan a family trip to Madrid with young kids unless you have no choice.
Expect a wet Spring season
In Madrid, it starts to rain starting March and carries on till May. So plan accordingly especially if you plan to travel in April which is the wettest month of the year. May tends to become much drier in comparison so you should be ok to travel as a family, with children of all ages.