Skip to content

Serializable DateTime

Ilia Petrov-Komotskii edited this page Sep 24, 2026 · 6 revisions

Serializable DateTime

UTC DateTime stored as a round-trip ISO 8601 string, plus a start–end range. Inspector fields get a text row and a calendar popup. Those inspectors are Property Drawers, so they own the field; Decorators can still wrap it.

using System;
using Mane.Unity;
using UnityEngine;

public class SeasonWindow : MonoBehaviour
{
    [SerializeField] private SerializableDateTime _opens;
    [SerializeField] private SerializableDateTimeRange _active;

    public void Use()
    {
        DateTime utc = _opens; // implicit
        _opens = DateTime.UtcNow;

        Debug.Log(_active); // "yyyy-MM-dd HH:mm – yyyy-MM-dd HH:mm"
    }
}

SerializableDateTime

  • Stored UTC. Invalid strings read as DateTime.UtcNow.
  • Unspecified DateTimeKind is treated as UTC. Local is converted with ToUniversalTime.
  • Normalize drops seconds and smaller units.
  • Display format yyyy-MM-dd HH:mm; date-only yyyy-MM-dd.
DateTime utc = SerializableDateTime.ToUtc(local);
string iso = SerializableDateTime.FormatRoundTrip(utc);
SerializableDateTime.TryParse(iso, out DateTime parsed);

SerializableDateTimeRange

Constructor swaps bounds if start is after end. Default range is now → a week later. StartString / EndString expose the raw ISO strings.

Calendar popup

The inspector button opens CalendarPopup (Mane.Unity.Editor): month grid, hour/minute sliders, UTC and local labels. Range mode has Start/End toggles; picking a start switches to end.

From editor code:

using Mane.Unity.Editor;

CalendarPopup.Show(DateTime.UtcNow, dt => Debug.Log(dt));
CalendarPopup.ShowRange(range, updated => serializedObject.ApplyModifiedProperties());

Screenshot: calendar-date.png — single-date popup (grid + time sliders). Host at https://raw.githubusercontent.com/wiki/ManeFunction/ManeTools-Unity/calendar-date.png.

Screenshot: calendar-range.png — range mode with Start/End buttons and hover highlight. Host at https://raw.githubusercontent.com/wiki/ManeFunction/ManeTools-Unity/calendar-range.png.

Clone this wiki locally