Total Time Played since Installation

First of all: I was looking around “answers” already, but couldn’t find exactly what I’m looking for. Whatever I tried is not giving me what I needed. So here I am asking for help! Sorry if I’m a total noob with coding… Anyway:

Basically I’d like my game to have a couple of statistics that the player can visualize on a menu.
One is called “Time Played Since Installation” and one is called “Time Played Since Start Game”.

The first should be a variable that is increasing every second the game is launched and running, despite what the player is doing and since the first run of the application (after installation), and keep going once the player come back after quitting (i.e. if he/she plays 2 hours today, and 1 hour tomorrow, the total time should be 3 hours).

The second is similar to the first, but it’s reset every time the player “Start a New Game” (and stored in the Saved files and increasing as well). Basically the only variation is that the second has a “reset to 0” when clicking “New Game”.

How can I obtain this? And also, how can I show the time format in Days/Hours/Minutes/Seconds?


Check out C#'s TimeSpan class. It has the properties Days, Hours, Minutes, and Seconds. TimeSpan Struct (System) | Microsoft Learn

You can store DateTime.Now upon starting the application/new game, and then subtract the stored DateTime from DateTime.Now upon closing the application/starting another new game. This will give you the TimeSpan of the current play session.

DateTime startTime = DateTime.Now;
/* user plays the game for a while */
TimeSpan playTime = DateTime.Now - startTime;

I would save play times using TimeSpan.ToString(), and load play times using new TimeSpan.Parse(str_previousPlayTime).

Hey anyone gets the answer to this question working? If yes then do tell me the correct solution, please.