How to keep the clock running even after the app is closed?

Or how do I detect, how long has it been since the app is closed?

Save the time in milliseconds in a tinyDB.
When the app is opened, read TinyDB and compare the current time witht the time saved.