I only think this is true if the hackathon is around projects that are part of or similar to the normal work. In those cases, they should be during work hours and treated as work.
My company 15 years ago or so did a hackathon with arduinos, where they provided a bunch of arduinos and hardware and food, but the projects we made were completely unrelated to work and served no practical purpose. My team made a Simon says game.
It was just for fun, there was no benefit for the company. I think those are fine.
A recent boss mandated that people come on weekends. Everyone’s contract said you have to, except mine. I pointed out to the boss that even though he can ask people to work on weekends, there are laws that prevent how much (you need more and longer breaks, and you can’t do it every weekend.)
He got cold feet and cancelled the event. But he forgot to tell people. The most junior developer had spent 2+ hours on the commute.
I’ve never heard of a company encouraging their employees to participate in a hackathon that they sponsored. Are you thinking of internal “hack week” periods where employees get a chance to build something “for fun” at work?