C# DateTime Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To calculate the number of days between two dates in C#, you can subtract the earlier date from the later date, and then access the Days
property of the resulting TimeSpan
. Here's an example:
DateTime date1 = new DateTime(2022, 3, 28); DateTime date2 = DateTime.Now; int days = (int)(date2 - date1).TotalDays;
In the above example, we have two DateTime
objects representing the two dates. We subtract the earlier date date1
from the later date date2
, and then access the TotalDays
property of the resulting TimeSpan
to get the total number of days between the two dates. We cast the result to an int
to get the whole number of days.
To calculate the number of business days (weekdays) between two dates in C#, you can use a loop to iterate over each day between the two dates, and then check if each day is a weekday using the DayOfWeek
property of the DateTime
object. Here's an example:
DateTime date1 = new DateTime(2022, 3, 28); DateTime date2 = DateTime.Now; int businessDays = 0; for (DateTime date = date1; date <= date2; date = date.AddDays(1)) { if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday) { businessDays++; } }
In the above example, we have two DateTime
objects representing the two dates. We then use a for
loop to iterate over each day between the two dates, incrementing the businessDays
variable for each weekday. We use the DayOfWeek
property of the DateTime
object to check if each day is a weekday, excluding Saturday and Sunday. We then get the resulting int
value which represents the number of business days between the two dates.
C# calculate days difference between two dates
Calculate the days difference between two dates in C# using TimeSpan.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2023, 12, 31); TimeSpan duration = endDate - startDate; int daysDifference = duration.Days;
DateTime subtraction for days difference in C#
Use DateTime subtraction for calculating the days difference between two dates in C#.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2023, 12, 31); int daysDifference = (endDate - startDate).Days;
Calculating date duration in days in C#
Calculate the date duration in days in C# using DateTime subtraction.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2023, 12, 31); int daysDuration = (endDate - startDate).Days;
Days between two DateTime objects in C#
Find the number of days between two DateTime objects in C# using TimeSpan.
DateTime date1 = new DateTime(2023, 1, 1); DateTime date2 = new DateTime(2023, 12, 31); int daysDifference = (date2 - date1).Days;
Calculate working days between two dates in C#
Calculate the working days (excluding weekends) between two dates in C#.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2023, 12, 31); int workingDays = CalculateWorkingDays(startDate, endDate); static int CalculateWorkingDays(DateTime startDate, DateTime endDate) { int workingDays = 0; DateTime currentDate = startDate; while (currentDate <= endDate) { if (currentDate.DayOfWeek != DayOfWeek.Saturday && currentDate.DayOfWeek != DayOfWeek.Sunday) { workingDays++; } currentDate = currentDate.AddDays(1); } return workingDays; }
C# date range and days difference
Calculate the days difference within a date range in C# using DateTime subtraction.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2023, 12, 31); int daysDifference = (endDate - startDate).Days;
Calculate age in days using DateTime in C#
Calculate the age in days using DateTime subtraction in C#.
DateTime birthdate = new DateTime(1990, 5, 15); DateTime currentDate = DateTime.Now; int ageInDays = (currentDate - birthdate).Days;
TimeSpan and days difference in C#
Use TimeSpan for calculating days difference between two DateTime objects in C#.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2023, 12, 31); TimeSpan duration = endDate - startDate; int daysDifference = duration.Days;
Handling negative days difference in C#
Handle negative days difference when the end date is before the start date in C#.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2022, 12, 31); int daysDifference = Math.Max(0, (endDate - startDate).Days);
C# days difference excluding weekends
Calculate days difference excluding weekends (working days) in C#.
DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2023, 12, 31); int workingDays = CalculateWorkingDays(startDate, endDate); static int CalculateWorkingDays(DateTime startDate, DateTime endDate) { int workingDays = 0; DateTime currentDate = startDate; while (currentDate <= endDate) { if (currentDate.DayOfWeek != DayOfWeek.Saturday && currentDate.DayOfWeek != DayOfWeek.Sunday) { workingDays++; } currentDate = currentDate.AddDays(1); } return workingDays; }