Technically, if you are cleared by immigration to board the plane before the expiry date then it's ok. So you have to be early to check-in and finish all the formalities before 12 midnight.