You normally cannot interact with animals while they are asleep, making it impossible to raise their heart level. The heart level of cows and goats, however, is increased through successful milking, which can be done any time, including while they are sleeping.
It is a known bug that sometimes Sometimes animals will refuse to go inside, moving around like they would during the day even at midnight. However this This can be fixed by leaving and re-entering the farm, and the animals will be inside.
*Tip: The advantage of milking after 8pm is that animals never walk away from you.