C# DateTime Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To get the start and end times of a day in C#, you can use the DateTime.Date
property to remove the time component of a DateTime
object, and then add or subtract a TimeSpan
of one day to get the start or end of the day, respectively. Here are some examples:
DateTime now = DateTime.Now; DateTime startOfDay = now.Date; // The start of the current day DateTime endOfDay = now.Date.AddDays(1).AddTicks(-1); // The end of the current day
To get the start day of a month in C#, you can create a DateTime
object for the first day of the month:
DateTime now = DateTime.Now; DateTime startOfMonth = new DateTime(now.Year, now.Month, 1);
To get the last day of a month in C#, you can create a DateTime
object for the first day of the next month, and then subtract one day:
DateTime now = DateTime.Now; DateTime endOfMonth = new DateTime(now.Year, now.Month, 1).AddMonths(1).AddDays(-1);
To get the last day of the next month in C#, you can create a DateTime
object for the first day of the month after next, and then subtract one day:
DateTime now = DateTime.Now; DateTime endOfNextMonth = new DateTime(now.Year, now.Month, 1).AddMonths(2).AddDays(-1);
To get the start day of the previous month in C#, you can create a DateTime
object for the first day of the current month, and then subtract one month:
DateTime now = DateTime.Now; DateTime startOfPreviousMonth = new DateTime(now.Year, now.Month, 1).AddMonths(-1);
To get the last day of the previous month in C#, you can create a DateTime
object for the first day of the current month, subtract one day to get the last day of the previous month, and then remove the time component:
DateTime now = DateTime.Now; DateTime endOfPreviousMonth = new DateTime(now.Year, now.Month, 1).AddDays(-1).Date;
To get the first day of a year in C#, you can create a DateTime
object for January 1st of the year:
DateTime now = DateTime.Now; DateTime startOfYear = new DateTime(now.Year, 1, 1);
To get the last day of a year in C#, you can create a DateTime
object for January 1st of the next year, and then subtract one day:
DateTime now = DateTime.Now; DateTime endOfYear = new DateTime(now.Year + 1, 1, 1).AddDays(-1);
C# get start time of DateTime:
To get the start time of a DateTime object, you can use the Date
property.
DateTime myDateTime = DateTime.Now; DateTime startTime = myDateTime.Date; Console.WriteLine($"Start time of DateTime: {startTime}");
Extracting time portion from DateTime in C#: Extracting the time portion of a DateTime can be done by subtracting the date part.
DateTime myDateTime = DateTime.Now; TimeSpan timeOfDay = myDateTime - myDateTime.Date; Console.WriteLine($"Time portion of DateTime: {timeOfDay}");
Get the beginning of the day in C# DateTime: Getting the beginning of the day is essentially getting the start time.
DateTime myDateTime = DateTime.Now; DateTime startOfDay = myDateTime.Date; Console.WriteLine($"Beginning of the day: {startOfDay}");
C# get end time of DateTime:
To get the end time of a DateTime object, you can use the Date
property and add 1 day.
DateTime myDateTime = DateTime.Now; DateTime endTime = myDateTime.Date.AddDays(1).AddTicks(-1); Console.WriteLine($"End time of DateTime: {endTime}");
Extracting date and time components in C#: Extracting both date and time components separately.
DateTime myDateTime = DateTime.Now; DateTime dateComponent = myDateTime.Date; TimeSpan timeComponent = myDateTime.TimeOfDay; Console.WriteLine($"Date Component: {dateComponent}, Time Component: {timeComponent}");
Start and end time of day in C# using DateTime: Combining the start and end time of the day.
DateTime myDateTime = DateTime.Now; DateTime startOfDay = myDateTime.Date; DateTime endOfDay = myDateTime.Date.AddDays(1).AddTicks(-1); Console.WriteLine($"Start of the day: {startOfDay}, End of the day: {endOfDay}");
C# DateTime start of week: Getting the start of the week involves finding the first day and time of the week.
DateTime myDateTime = DateTime.Now; DayOfWeek startOfWeek = DayOfWeek.Monday; // or Sunday DateTime startOfWeekDate = myDateTime.Date.AddDays(-(int)myDateTime.DayOfWeek - (int)startOfWeek); Console.WriteLine($"Start of the week: {startOfWeekDate}");
Getting midnight time from DateTime in C#: Retrieving midnight time is equivalent to getting the start time of the day.
DateTime myDateTime = DateTime.Now; DateTime midnightTime = myDateTime.Date; Console.WriteLine($"Midnight time: {midnightTime}");
Retrieve time component without date in C#: If you only want the time without the date, extract the time portion.
DateTime myDateTime = DateTime.Now; TimeSpan timeWithoutDate = myDateTime.TimeOfDay; Console.WriteLine($"Time without date: {timeWithoutDate}");
Extracting hours and minutes from DateTime in C#: Extracting specific components like hours and minutes.
DateTime myDateTime = DateTime.Now; int hours = myDateTime.Hour; int minutes = myDateTime.Minute; Console.WriteLine($"Hours: {hours}, Minutes: {minutes}");