Adding different amounts of days for different months in date system

I followed a youtube tutorial and got a date system (not using computer date, a “fake” date system) working but the only problem is that each month has a different amount of days and I don’t know how to make it so where that is the case and currently it only has a certain amount of days per each month. How do I do that

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

public class ScriptDate : MonoBehaviour
{
    public TextMeshProUGUI[] UI_DATE_TEXT;
    public DateFormat dateFormat = DateFormat.MM_DD_YYYY;
    public float secPerDay = 2;

    private string _date;

    int day;
    int month;
    int year;

    int maxDay = 30;
    int maxMonth = 13;

    float timer = 0;

    public enum DateFormat
    {
        MM_DD_YYYY
    }

    private void Awake()
    {
        day = 1;
        month = 1;
        year = 2000;

        SetDateString();
    }
   


    void Update()
    {
        if (timer >= secPerDay)
        {
            day++;
            if (day >= maxDay)
            {

                day = 1;
                month++;
                if (month >= maxMonth)
                {

                    month = 1;
                    year++;

                }

            }

            SetDateString();

            timer = 0;

        }
        else
        {
            timer += Time.deltaTime;
        }
    }

    void SetDateString()
    {
        switch(dateFormat)
        {
            case DateFormat MM_DD_YYYY:
                {
                    _date = month + "/" + day + "/" + year;
                    break;
                }
        }
        for (int i = 0; i < UI_DATE_TEXT.Length; i++)
        {
            UI_DATE_TEXT[i].text = _date;
        }
    }
}

You can create an array holding the maximum days for each month like this:

private int[] maxDay = new int[] {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

Then in your Update() when you ask if(day >= maxDays) you can instead use the given value for the current month: if(day >= maxDay[month-1])

void Update()
    {
        if (timer >= secPerDay)
        {
            day++;
            if (day >= maxDay[month-1])
            {
                day = 1;
                month++;
                if (month >= maxMonth)
                {
                    month = 1;
                    year++;
                }
            }

            SetDateString();
            timer = 0;
        }
        else
        {
            timer += Time.deltaTime;
        }
    }
4 Likes