Game Time

Hi,

Sorry, I honestly have looked for an answer for this.

I’m trying to have my game time/calendar run quicker that real time but initialized from a real world time origin.

So that the game isn’t taking its origin time from Delta time and time will have passed even when not playing.

So my thought was, set an origin time, work out how many seconds have passed since then and DateTime.Now then simply apply my multiplier and somehow format it (where I got confused).

I’d like to set this real world time (origin in the below example as the dawn of time in game so to speak, and every 0.4 RL second increment the game time by 1 second)

Hopefully, this all make some sense.

Does anyone know what my next step is?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class DisplayTimeOnText : MonoBehaviour
{

    int secondsPerYear = 360 * 24 * 60 * 60;  // 360 days per year
    int secondsPerMonth = 30 * 24 * 60 * 60;  // 30 days per month
    int secondsPerWeek = 8 * 24 * 60 * 60;    // Eight Days a Week
    int secondsPerDay = 24 * 60 * 60;         // 24 hours per day
    int secondsPerHour = 60 * 60;             // 60 minutes per hour

    /*
     
     Game Times:

                1 Game second = .04 Seconds (1/25th of a second)
                1 Game Minute = 2.4 seconds
                1 Game Hour = 2 minutes 24 seconds
                1 Game  Day = 57 minutes 36 seconds
                1 Game  Week = 7 hours 40 minutes 48 seconds
                1 Game  Month = 1 day 4 hours 48 minutes
                1 Game  Season = 3 days 14 hours 24 minutes
                1 Game Year = 14 days 9 hours 36 minutes


    */

    DateTime origin = new DateTime(2008, 5, 1, 8, 30, 52);


    System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
    System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
    System.DateTime dateNow = System.DateTime.Now;

    private Text clockText;

    // Use this for initialization
    void Start ()
    {
        clockText = GetComponent<Text>();




    }
   
    // Update is called once per frame
    void Update ()
    {
       
        System.TimeSpan diff1 = date2.Subtract(date1);

        clockText.text = diff1.ToString();
    }



}

I didn’t really read your code, but .4 seconds is like: 1s = 2.5 pretend seconds.
aka. time.deltaTime * 2.5f

TimeSpawn from the start (on next load) * 2.5f …

etc…

Once you’ve done “using System;” , you needn’t type: System.DateTime or System.TimeSpan
I mean, you can, but you don’t have to :slight_smile: (Barring some conflict…)

Is that helpful?

You could use Time.time to get the total number of seconds, scale it with some multiplier, then format it if needed with DateTime/TimeSpan? Or were you looking for the TimeSpan class, which is useful for handling delta times?

Or use System.Diagnostics.Stopwatch which gives you all sorts of useful info such as milliseconds, seconds, etc.