HEIGHT ABOVE SEA LEVEL
  • Articles
  • Quizzes
Join Patreon

Suggested

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon
    • Articles
    • Quizzes
    Join Patreon

    Here's some dummy text to automatically resize the container as if there were actual text

    Some dummy text that's hidden to resize the component automatically

    Performance

    Audio

    Become a patreon member and receive the following:

    HEIGHT ABOVE SEA LEVEL
    • Articles
    • Quizzes
    Join Patreon

    Suggested

    • constants headerA Game Developer's Guide to Constants in C#December 12, 2025
    • A reflection probe in front of a house in Unity.Reflection Probes in Unity: How to Get the Most Out of ThemNovember 2, 2025
    • How to Bake Lights in Unity: A Step-by-Step GuideNovember 2, 2025
    • strings header imageA Game Developer's Guide to Strings in C#December 12, 2025
    • A Game Developer's Guide to Loops in C#December 12, 2025

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon
    • Articles
    • Quizzes
    Join Patreon

    Question of the Day

    What value will isLearningCSharp have in this code?

    var isLearningCSharp = !true || false;

    Explanation

    Only one option has to be true for the variable to be true, but since both are false, the variable ends up being false. I hope you're learning C# though!

    Click here to learn the truth about Booleans in C#

    Think you know game dev? Check out the quizzes and find out.

    A Game Developer's Guide to Dates and Times in C#

    A Game Developer's Guide to Dates and Times in C#

    • Beginner C#
    • Set Up & Getting Started
    Victor Nyagudi
    Victor Nyagudi

    December 12, 2025

    Read this article ad-free by becoming a Patreon member

    Metal Gear Solid 3: Snake Eater had a creative feature that few expected when it was released on the PlayStation 2 back in 2004.

    It had a boss fight where, if you closed the game and didn't play it for two weeks, the boss would die of old age, and you'd proceed to the next level.

    Incidentally, if you closed the game and set the console's date to two weeks in the future, the same thing would happen.

    Here's one likely approach Konami took to implement the feature.

    The code accesses the console's date when the player reaches the boss fight, saves it, and, whenever the player relaunches the game later, compares the current date to the date they first reached the boss fight level.

    If the time between exceeds or matches two weeks, the code sets a Boolean value, such as bossIsDefeated to true, and the player proceeds to the next section.

    It's a very clever way to use dates reminiscent of what you'd expect from Hideo Kojima himself.

    You don't have to be as creative with dates when working in the Unity Game Engine, but knowing how to use them can go a long way.

    This article assumes you know how to work with classes and methods in C#.

    If you're a visual learner, scroll to the bottom for the video version, or you can watch it on YouTube.

    Dates in C#

    C# provides a struct called DateOnly you can use to work with dates. Think of a struct as a mini class.

    You can create an instance of this struct and provide the day, month, and year to create a new date.

    var releaseDate = new DateOnly(2025, 12, 7);
    
    Debug.Log(releaseDate);
    
    // Output: 12/7/2025 (Defaults to US date format)

    DateOnly has some helpful methods like AddDays and AddMonths if you want to update the date object you created.

    You can even convert the date to the extended version, where the day and month are spelled out instead of numbers separated by forward slashes.

    var releaseDate = new DateOnly(2025, 12, 7);
    
    Debug.Log(releaseDate); // Logs '12/7/2025'.
    
    // Logs 'Sunday, December 7, 2025'.
    Debug.Log(releaseDate.ToLongDateString());
    
    Debug.Log(releaseDate.AddMonths(2)); // Logs '2/7/2026'.

    Time in C#

    The TimeOnly struct in C# lets you work with different times of the day. You can create an instance of it and specify the hours, minutes, and seconds of a time.

    var startTime = new TimeOnly(10, 30, 22);
    
    Debug.Log(startTime); // Logs '10:30 AM'.

    TimeOnly also offers helpful methods like AddHours and AddMinutes to advance the time, and methods like ToLongTimeString if you want to see the seconds together with the hours and minutes.

    var startTime = new TimeOnly(10, 30, 22);
    
    Debug.Log(startTime); // Logs '10:30 AM'.
    
    Debug.Log(startTime.ToLongTimeString()); // Logs '10:30:22 AM'.
    
    Debug.Log(startTime.AddMinutes(15)); // Logs '10:45 AM'.

    Tip

    TimeOnly is different from the Time class Unity provides. TimeOnly handles the hardware's time and any time you explicitly declare, while Unity's Time class handles in-game time as the game runs.

    Working with Both Dates and Times

    If you want to use both dates and times, you can use the DateOnly struct.

    It combines the methods and properties of the DateOnly and TimeOnly structs for easier access and use.

    var dateAndTime = new DateTime(2025, 12, 7, 10, 30, 22);
    
    Debug.Log(dateAndTime); // Logs '12/7/2025 10:30:22 AM'.

    Time Spans in C#

    C# has a TimeSpan struct for working with time spans that can be helpful if you need to know the amount of time that has passed between two dates.

    You can set a time span manually by creating an instance of the TimeSpan struct or by subtracting one DateTime object from another.

    The TimeSpan object has helpful properties like TotalHours and TotalMinutes for a more granular perspective of the time that has passed.

    var timespan = new TimeSpan(5, 10, 00);
    
    Debug.Log(timespan); // Logs '05:10:00'.
    
    Debug.Log(timespan.TotalMinutes); // Logs '310'.

    If you need to, you can use the DateOnly, TimeOnly, DateTime, and TimeSpan structs in fields and properties, method parameters, or as method return types.

    public class Dashboard
    {
      private DateTime _dateTime; // <- 'DateTime' field.
    
      public TimeOnly Time { get; set; } // <- 'TimeOnly' property.
    
      public DateOnly GetDate() // <- Returns 'DateOnly' type.
      {
        return new DateOnly(2025, 6, 3);
      }
    }

    You don't have to use these structs to implement dates and times in your Unity game. They're merely one of many ways to approach it.

    You could alternatively use a counter that counts up to 7 and assigns a day of the week to each number.

    Every 20 minutes, the counter increases by 1, and the current day updates to the next one in the enum or list. The counter then resets to 1 after reaching 7.

    Recap

    • The DateOnly struct provides methods and properties to work with dates alone.
    • The TimeOnly struct provides methods and properties to work with times alone.
    • The DateTime struct lets you work with both dates and times.
    • The TimeSpan struct lets you work with time spans.
    • All the date and time structs work with hardware dates or explicitly declared dates, while the Unity-provided Time class handles in-game time.

    This article is the last in a series breaking down common C# concepts used in game development with the Unity Game Engine.

    Remember to check the other articles on baking lights to learn how to light up your levels in Unity.

    Here's the complete list of articles in the series:

    • A Game Developer's Guide to Variables in C#
    • A Game Developer's Guide to Constants in C#
    • A Game Developer's Guide to Numbers in C#
    • A Game Developer's Guide to Operators in C#
    • A Game Developer's Guide to Strings in C#
    • A Game Developer's Guide to Booleans in C#
    • A Game Developer's Guide to Enums in C#
    • A Game Developer's Guide to Conditional Statements in C#
    • A Game Developer's Guide to Loops in C#
    • A Game Developer's Guide to Classes in C#
    • A Game Developer's Guide to Access Modifiers in C#
    • A Game Developer's Guide to Fields & Properties in C#
    • A Game Developer's Guide to Methods in C#
    • A Game Developer's Guide to Lists & Arrays in C#
    • A Game Developer's Guide to Dates & Times in C# (you are here)

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon

    Latest

    • A Game Developer's Guide to Dates and Times in C#December 12, 2025
    • A Game Developer's Guide to Lists and Arrays in C#December 12, 2025
    • A Game Developer's Guide to Methods in C#December 12, 2025

    Suggested

    • constants headerA Game Developer's Guide to Constants in C#December 12, 2025
    • A reflection probe in front of a house in Unity.Reflection Probes in Unity: How to Get the Most Out of ThemNovember 2, 2025
    • How to Bake Lights in Unity: A Step-by-Step GuideNovember 2, 2025
    • strings header imageA Game Developer's Guide to Strings in C#December 12, 2025
    • A Game Developer's Guide to Loops in C#December 12, 2025

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon