This question already has an answer here:
We know students aren't allowed to do magic during the school holidays, but when does that prohibition start and end? Does the journey between Hogwarts and London count as term-time or holiday? Are students allowed to cast spells on the Hogwarts Express?
The only canonical example I can think of is Hermione casting spells on the train before the start of her first year, when the ban on magic isn't yet in operation because she hasn't even enrolled at the school yet. I haven't got the books to hand, so can't search for other examples.