How to save DateTime in a list

How to save DateTime in a list
Hello Guys, how I can save my date time every time I play from recent to old date-time. When I click at the start button it will show like this. What I want to do is new date-time will be at the top and old-time move to the bottom. So basically when I click, the first one move to the second and the first one will take new date-time. For now this all I have done.

{

    timer = DateTime.Now.ToString("dd/MM/yy    hh:mm tt");
    timeText5.text = timer;
    timeText1.text = timer;
    timeText4.text = timer;
    timeText3.text = timer;
    timeText2.text = timer;
    timeText1.text = timer;

    PlayerPrefs.SetString("timer", timer);
    PlayerPrefs.GetString("timer");    

    InvokeRepeating("Timer", 60.0f, 0.3f);  

   
}

void Timer()
{
    
    timer = DateTime.Now.ToString("dd/MM/yy    hh:mm tt");
    
    timeText1.text = timer;
    
} 

151502-screenshot-1.png

The best way would be to store all that stuff in an xml file containing all the save data.

Simply have a Queue containing string representation of your dates, in a serializable class. You can follow this tutorial to get an understanding of how that would work.


Every time the game starts, Enqueue the new date, and pop the last one. Then display the queue in your Text element.

To do that, your code would look like this:

public Text dateDisplay;
public GameData game;

//...

void Start()
{
    //deserialize your class here

    //add the new date
    game.dates.Enqueue(DateTime.Now.ToString("dd/MM/yy         hh:mm tt"));
    //if too many dates are added, remove the oldest one
    if (game.dates.Count > 5)
        game.dates.Dequeue();

    //concatenate all dates into one string
    String display = "";
    foreach (String s in game.dates)
         display += s + "

";

    //display the string
    textDisplay.text = display;
}

If you still want to use PlayerPrefs, you can instead use this to load and save a queue

//loading
Queue<String> dates = new Queue<String>();
for (int i = 0; i < 5; i++)
{
    String date = PlayerPrefs.GetString("date" + i, String.Empty);
    if (date.IsNullOrEmpty() == false)
        dates.Enqueue(date);
}

//save
int i = 0;
foreach (String s in dates)
{
    PlayerPrefs.SetString("date" + i, s);
    i++;
}
}