A better understanding of how livestock respond to weather is essential to enable farming to adapt to a changing climate. Climate change is mainly expected to impact dairy cattle through heat stress and an increase in the frequency of extreme weather events. We investigated the effects of weather on milk yield and composition (fat and protein content) in an experimental dairy herd in Scotland over 21 years. Holstein Friesian cows were either housed indoors in winter and grazed over the summer or were continuously housed. Milk yield was measured daily, resulting in 762 786 test day records from 1369 individuals, and fat and protein percentage were sampled once a week, giving 89 331 records from 1220 cows/trait. The relative influence of 11 weather elements, measured from local outdoor weather stations, and two indices of temperature and humidity (THI), indicators of heat stress, were compared using separate maximum likelihood models for each element or index. Models containing a direct measure of temperature (dry bulb, wet bulb, grass or soil temperature) or a THI provided the best fits to milk yield and fat data; wind speed and the number of hours of sunshine were most important in explaining protein content. Weather elements summarised across a week’s timescale from the test day usually explained milk yield and fat content better than shorter-scale (3 day, test day, test day −1) metrics. Then, examining a subset of key weather variables using restricted maximum likelihood, we found that THI, wind speed and the number of hours of sunshine influenced milk yield and composition. The shape and magnitude of these effects depended on whether animals were inside or outside on the test day. The milk yield of cows outdoors was lower at the extremes of THI than at average values, and the highest yields were obtained when THI, recorded at 0900 h, was 55 units. Cows indoors decreased milk yield as THI increased. Fat content was lower at higher THIs than at intermediate THIs in both environments. Protein content decreased as THI increased in animals kept indoors and outdoors, and the rate of decrease was greater when animals were outside than when they were inside. Moderate wind speeds appeared to alleviate heat stress. These results show that milk yield and composition are impacted at the upper extreme of THI under conditions currently experienced in Scotland, where animals have so far experienced little pressure to adapt to heat stress.