Like the similar question https://stackoverflow.com/a/21482718/1256925, you can do this the following way:
var now = new Date();
var night = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1, // the next day, ...
0, 0, 0 // ...at 00:00:00 hours
);
var msTillMidnight = night.getTime() - now.getTime();
setTimeout('document.location.refresh()', msTillMidnight);
The only difference here is the final timer that runs a script. The rest of the script works the same as in that other question.
You can change the value for var night
to the following if you want to use UTC instead of the user's local time:
var night = new Date(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate() + 1, // the next day, ...
0,
now.getTimezoneOffset(), //this returns the amount of minutes difference in timezones
0
);