[csharp] Calendar

Viewer

copydownloadembedprintName: Calendar
  1. using System;
  2. using TMPro; 
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Calendar : MonoBehaviour
  7. {
  8.     [Header("Start elements")]
  9.     [SerializeField] private TMP_Text startMonthText;
  10.     [SerializeField] private TMP_Text startDayText;
  11.     [SerializeField] private TMP_Text startYearText;
  12.     [SerializeField] private Button startMonthUp;
  13.     [SerializeField] private Button startMonthDown;
  14.     [SerializeField] private Button startDayUp;
  15.     [SerializeField] private Button startDayDown;
  16.  
  17.     [Header("End elements")]
  18.     [SerializeField] private TMP_Text endMonthText;
  19.     [SerializeField] private TMP_Text endDayText;
  20.     [SerializeField] private TMP_Text endYearText;
  21.     [SerializeField] private Button endMonthUp;
  22.     [SerializeField] private Button endMonthDown;
  23.     [SerializeField] private Button endDayUp;
  24.     [SerializeField] private Button endDayDown;
  25.  
  26.     private DateTime startDate;
  27.     private DateTime endDate;
  28.     private DateTime currentDate;
  29.  
  30.     private void Start()
  31.     {
  32.         currentDate = DateTime.Now;
  33.         startDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day);
  34.         endDate = startDate.AddDays(1);
  35.  
  36.         InitializeUI();
  37.     }
  38.  
  39.     private void InitializeUI()
  40.     {
  41.         startMonthUp.onClick.AddListener(() => AdjustStartDate(month: 1));
  42.         startMonthDown.onClick.AddListener(() => AdjustStartDate(month: -1));
  43.         startDayUp.onClick.AddListener(() => AdjustStartDate(day: 1));
  44.         startDayDown.onClick.AddListener(() => AdjustStartDate(day: -1));
  45.  
  46.         endMonthUp.onClick.AddListener(() => AdjustEndDate(month: 1));
  47.         endMonthDown.onClick.AddListener(() => AdjustEndDate(month: -1));
  48.         endDayUp.onClick.AddListener(() => AdjustEndDate(day: 1));
  49.         endDayDown.onClick.AddListener(() => AdjustEndDate(day: -1));
  50.  
  51.         UpdateStartUI();
  52.         UpdateEndUI();
  53.     }
  54.  
  55.     private void AdjustStartDate(int month = 0int day = 0)
  56.     {
  57.         startDate = startDate.AddMonths(month).AddDays(day);
  58.         ValidateDate(ref startDate);
  59.         UpdateStartUI();
  60.         ValidateEndDate();
  61.     }
  62.  
  63.     private void AdjustEndDate(int month = 0int day = 0)
  64.     {
  65.         endDate = endDate.AddMonths(month).AddDays(day);
  66.         ValidateDate(ref endDate);
  67.         UpdateEndUI();
  68.     }
  69.  
  70.     private void ValidateDate(ref DateTime date)
  71. {
  72.     if (date < currentDate)
  73.     {
  74.         date = currentDate;
  75.     }
  76.     // Ensure the date is not beyond a reasonable future date(I put 5 but you can change it to whatever you like)
  77.     else if (date > currentDate.AddYears(5))
  78.     {
  79.         date = currentDate.AddYears(5);
  80.     }
  81. }
  82.  
  83.     private void UpdateStartUI()
  84.     {
  85.         startMonthText.text = startDate.Month.ToString();
  86.         startDayText.text = startDate.Day.ToString();
  87.         startYearText.text = startDate.Year.ToString();
  88.     }
  89.  
  90.     private void UpdateEndUI()
  91.     {
  92.         endMonthText.text = endDate.Month.ToString();
  93.         endDayText.text = endDate.Day.ToString();
  94.         endYearText.text = endDate.Year.ToString();
  95.     }
  96.  
  97.     private void ValidateEndDate()
  98.     {
  99.         if (endDate < startDate)
  100.         {
  101.             endDate = startDate.AddDays(1);
  102.             UpdateEndUI();
  103.         }
  104.     }
  105. }

Editor

You can edit this paste and save as new:


File Description
  • Calendar
  • Paste Code
  • 03 Jul-2024
  • 3.23 Kb
You can Share it: