C# Date and Time


This entry is part 7 of 8 in the series C# Complex Types

There is a type called DateTime that is defined in the System namespace that represents an exact moment in time. DateTime is a structure. One way to create a DateTime object is to use the new operator. The constructor of this object has multiple overloads. For example you could use the year month day overload. Alternatively you could use the year month day hour minute second overload, which accept an integer.

If you want to access the current date and time you use DateTime.Now. This gets a DateTime object that is set to the current date and time on this computer, expressed as the local time. There are three properties of DateTime: Now, Today and UtcNow. Now gets the current date. UtcNow gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).

DateTime objects in C# are immutable. There are some methods you can use to change it. All of these methods start with Add. If you need to subtract instead of add, just use a negative number.

Formatting DateTime to a String

We will use our now object to show examples in the code below. Here we use a format specifier. There are lots and lots of them. To see a full list from Microsoft go to the page called Custom Date and Time Format Strings.

using System;

namespace DateTime
{
    class Program
    {
        static void Main(string[] args)
        {

            var dateTime = new DateTime(2000, 1, 1);
            var now = DateTime.Now;  // gets the current date and time
            var today = DateTime.Today;  // gets the current date (no time)
            var utcnow = DateTime.UtcNow;

            Console.WriteLine($"The current hour is: {now.Hour}");
            Console.WriteLine($"The current minute is:  {now.Minute}");
            Console.WriteLine($"The current second is:  {now.Second}");

            var tomorrow = now.AddDays(1);
            var yesterday = now.AddDays(-1);

            Console.WriteLine($"Tomorrow (yyyy-mm-dd): {tomorrow}");

            Console.WriteLine(now.ToLongDateString());
            Console.WriteLine(now.ToShortDateString());
            Console.WriteLine(now.ToLongTimeString());
            Console.WriteLine(now.ToShortTimeString());
            Console.WriteLine(now.ToString());  // shows date and time
            Console.WriteLine(now.ToLongDateString() + " " + now.ToLongTimeString());
            Console.WriteLine(now.ToString("yyyy-MM-dd"));  // format specifier
            Console.WriteLine(now.ToString("yyyy-MMMM-dd"));  // format specifier
            Console.WriteLine(now.ToString("dddd yyyy-MMMM-dd"));  // format specifier
            Console.WriteLine(now.ToString("yyyy-MM-dd HH:mm:ss"));
            // Google: C# DateTime format specifier for more info
        }
    }
}

Here is the output.

OLE Automation Date

What is the OLE Automation date and why would we ever need to work with it? Let’s start with the second question. Suppose you are building a Window program using C# and you want to use a SQLite database. The SQLite database can be included in your project and can be shipped with your distribution. It’s local. SQLite does not support built-in date and/or time storage class. Instead, it leverages some built-in date and time functions to use other storage classes such as TEXT, REAL, or INTEGER for storing the date and time values. Have a look at the article called SQLite Date & Time at SQLite Tutorial.

We can store dates in the SQLite database using the REAL data type. For example, November 23, 2020, is 44158 as an OLE Automation date in C#. An OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899. In SQLite, the day is the number of days since noon in Greenwich on November 24, 4714 B.C. based on the proleptic Gregorian calendar. The difference between the two is 2415019. In SQLite, November 23, 2020, is 2459177. For this date, we will be storing 2459177 in the database as a REAL number. We’ll then want to translate that back to the C# DateTime date by subtracting 2415019 from 2459177 to get 44158. Why would we do this? We can use 44158 to display that date to the user as “November 23, 2020”.

Here are a couple of lines of code as an example.

DateTime today = DateTime.Now;
double td = today.ToOADate();

Series Navigation<< C# Structs with FunctionsC# TimeSpan >>