A simple way to extract only the "month" or "hour" from a C# DateTime
When handling dates and times in C#, a DateTime object holds the date and time as a single set, such as "November 13, 2025, 15:30:45".
However, in actual development, rather than using the entire DateTime, there are many situations where you need only specific date/time elements (components) individually, such as:
"I want to get only the **'month'** to perform monthly aggregation."
"I want to extract only the **'hour'** to analyze logs for a specific time period."
"I want to determine the **'day of the week'** to change processing between weekends and weekdays."
These are very common requirements.
The convenient 'property' feature of DateTime
To meet these needs, DateTime objects come standard with "properties" that allow direct access to each element. By simply accessing these properties, you can easily extract the value of the desired part as an int (integer) type.
.Year (get the year)
.Month (get the month, 1-12)
.Day (get the day, 1-31)
.Hour (get the hour, 0-23)
.Minute (get the minute, 0-59)
.Second (get the second, 0-59)
.Millisecond (get the millisecond, 0-999)
You can handle them very intuitively in if statements or calculations, such as if (logTimestamp.Hour == 15).
What if you only want the date or only the day of the week?
DateTime provides even more convenient properties.
The .Date property When you access it like logTimestamp.Date, you can get a new DateTime object that represents the date of that day, with the time information (hours, minutes, seconds) neatly truncated (set to 00:00:00). This is extremely useful when comparing or aggregating data by date.
The .DayOfWeek property When you access it like logTimestamp.DayOfWeek, it returns the day of the week for that date as a dedicated 'enum', such as DayOfWeek.Sunday or DayOfWeek.Monday. This allows for very readable day-of-the-week checks, such as if (weekDay == DayOfWeek.Saturday).
Read more on the blog: Concrete C# code and execution results
In this article, I introduced the 'concept' of 'properties' for extracting individual elements from a DateTime object.
You might be thinking, 'So, what is the actual C# sample code to get logTimestamp.Year or logTimestamp.Hour?' 'I want to see code that confirms how the time becomes 0:00:00 when using the .Date property.' 'What is the specific way to write an if statement using the .DayOfWeek property to determine if it is a weekday or a weekend?'
Some of you may have thought this.
On the technical blog I run, as a continuation of this article, I explain in detail the specific C# sample code to create a DateTime object, get each property from .Year to .Millisecond, and obtain .Date and .DayOfWeek, along with their **execution results**.
These are basic techniques for freely handling date and time data. For details, please check the link below.
▼ Click here for the blog post [C#] List of properties to individually get year, month, day, hour, minute, and second from DateTime
