Humans are diurnal--we are active in the day and sleep at night. But diurnalism is by far the exception rather the rule in mammals. About 250-230 million years ago, the mammalian ancestors, called the therapsids, became exclusively nocturnal, and stayed so until the demise of the dinosaurs 66 million years ago. All of our mammal ancestors lived in the dark for about 200 years, and the majority still do to this day. Humans are, essentially, nocturnal animals that have reverted back to living in the sun.
There has been much speculation about why the therapsids became nocturnal. The traditional argument is that the archosauriforms and the dinosaurs became ecologically dominant during the Triassic. To avoid being eaten by the multitude of new carnivorous reptiles, the archaic mammals, it is argued, fled into the dark, where reptiles had yet to dominate. In a new paper, "Obligatory nocturnalism in Triassic archaic mammals: Preservation of sperm quality?" (Physiological and Biochemical Zoology 92, no. 6 (November/December 2019): 544-553), Barry G. Lovegrove proposes a simple, new, alternative hypothesis based purely upon physiological constraints.
The therapsids were becoming rapidly endothermic (producing more of their own internal heat through metabolism) to fuel new energy demands and to defend the consequent elevated body temperature, especially as they got smaller during the Triassic. And herein lies a problem. As their body temperature started to approach that of the air, around 93.2°F (34°C), they would not have been able to offload excessive heat generated by being active during the day without losing vast amounts of body water through evaporative cooling, such as by sweating or panting.
Archaic mammals did not have scrotums, in which the testes are kept cool, and if there had not been a way to keep sperm cool, quality would have declined through the accumulation of free radicals with the increases in temperature during sperm maturation. By becoming active during the cooler nights, these mammals were able to preserve sperm quality. A nocturnal lifestyle could solve this problem, now that they were "warm-blooded," with the newly acquired thermoregulatory toolkit to cope with the cooler night air.