Days since first launch

I want to count the days since the first launch of my game. I’m not talking about playing time, I’m talking about the total days that have passed, to display some statistics when the player finishes the game. How can I do this?

I suppose it has something to do with System.DateTime, but I’m wondering exactly what I would have to implement.

The very first time the game was launched it would have to store that day’s System.Datetime.Today in a variable (of type DateTime?) and then do the same at the day someone would finish the game and then subtract the two and get an integer as a result?

If so, how would I ensure that the date of the first launch would only be stored once and not every time the game is launched?

I wasn’t exactly sure how to do this either until I read your question a few minutes ago and used some voodoo known as Google.

I threw together a script just now using PlayerPrefs that seems to be working. I changed my PC’s date a few times to test it out. Anyway, here ya go.

Put this script (rename it to whatever, of course) on some kind of gameManager object you’re using in your game.

public class WhateverScriptNameYouWant: MonoBehaviour

	private static System.DateTime startDate ;
	private static System.DateTime today ;

	void Start()
		SetStartDate() ;

	void SetStartDate()
		if(PlayerPrefs.HasKey("DateInitialized")) //if we have the start date saved, we'll use that
			startDate = System.Convert.ToDateTime(PlayerPrefs.GetString("DateInitialized")) ;
		else //otherwise...
			startDate = System.DateTime.Now ; //save the start date ->
			PlayerPrefs.SetString("DateInitialized", startDate.ToString()) ;

	public static string GetDaysPassed()
		today = System.DateTime.Now ;

		//days between today and start date -->
		System.TimeSpan elapsed = today.Subtract(startDate) ;

		double days = elapsed.TotalDays ;

		return days.ToString("0") ;


Then from whatever other script(s) you’re using to show the days passed, you just call it.

eg :

void OnGUI()
   GUILayout.Label("Days Passed : " + WhateverScriptNameYouWant.GetDaysPassed()) ;

You could store it in a text file that could be read by the game when they finish. And in order to make it do it only once you could have it check to see if the file was already there.

I tried this out but for some reason overtime I start up my game session the OnGUI spits out this insane number something like: 736333 instead of what I think I was saving with the SaveStartDate…
I thought the way it was supposed to work was save the start date and then subtract the current date from the start date in the OnGUI… instead I get this each time85322-screenshot-2017-01-04-151205.png

While @loonhawks answer is great that opens up a few hacks, The user could easily change their system time thus work around what you’re trying to do. He even went on to test it that way.

I suggest you use his answer as a backup if the user has no Internet access, if they have Internet access get your date time from the Internet:

public static DateTime GetNistTime()
    DateTime dateTime = DateTime.MinValue;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
    request.Method = "GET";
    request.Accept = "text/html, application/xhtml+xml, */*";
    request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
    request.ContentType = "application/x-www-form-urlencoded";
    request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
        StreamReader stream = new StreamReader(response.GetResponseStream());
        string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
        string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
        double milliseconds = Convert.ToInt64(time) / 1000.0;
        dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();

    return dateTime;