1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// Дата в выпадающем списке
- /// </summary>
- public class DateTimePicker : MonoBehaviour
- {
- public Dropdown Day;
- public Dropdown Month;
- public Dropdown Year;
- public DateTime Date {
- get {
- var date = new DateTime(int.Parse(Year.options[Year.value].text), int.Parse(Month.options[Month.value].text), int.Parse(Day.options[Day.value].text));
- return date; }
- set {
- Days();
- //Year.value = Date.Year.;
- Month.value = Date.Month - 1;
- Day.value = Date.Day - 1;
- oldYear = Year.value;
- oldMonth = Month.value;
- }
- }
- int oldYear = -1;
- int oldMonth = -1;
- // Start is called before the first frame update
- void Start()
- {
- DateNow();
- }
- // Update is called once per frame
- void Update()
- {
- if (oldYear == -1 || oldMonth == -1)
- DateNow();
- if (oldYear != Year.value || oldMonth != Month.value)
- {
- Days();
- oldYear = Year.value;
- oldMonth = Month.value;
- }
- }
- void Days()
- {
- Day.options.Clear();
- var year = int.Parse(Year.options[Year.value].text);
- var month = int.Parse(Month.options[Month.value].text);
- var days = DateTime.DaysInMonth(year, month);
- for (int d = 1; d <= days; d++)
- Day.options.Add(new Dropdown.OptionData($"{d}"));
- Day.value = 0;
- }
- void DateNow()
- {
- for (var y = 2020; y <= DateTime.Now.Year; y++)
- Year.options.Add(new Dropdown.OptionData($"{y}"));
- for (var m = 1; m <= 12; m++)
- Month.options.Add(new Dropdown.OptionData($"{m}"));
- Year.value = Year.options.Count - 1;
- Month.value = DateTime.Now.Month - 1;
- oldYear = Year.value;
- oldMonth = Month.value;
- Days();
- Day.value = DateTime.Now.Day - 1;
- }
- }
|