I want to calculate the numbers of days and nights between two dates. My date fields are in string types. I am trying to subtract like that but the sing "-" gives error. My code is like that.
(DateTime.Parse(dr["dtarihi"].ToString()).ToShortDateString()) - (DateTime.Parse(dr["dtarihi"].ToString()).ToShortDateString())

Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 13, 2023, 10:28 PM EDT
please try the below code
DateTime startDate = new DateTime(2023, 5, 1); // Example start date DateTime endDate = new DateTime(2023, 6, 10); // Example end date TimeSpan duration = endDate - startDate; int totalDays = duration.Days; int totalNights = duration.Days + 1; // Assuming nights refer to full 24-hour periods Console.WriteLine("Total days: " + totalDays); Console.WriteLine("Total nights: " + totalNights);Mehmet FatihRank #1,474 · 1.1K reputationPosted Jun 14, 2023, 5:44 AM EDT
First of all, thank you very much for your interest. The problem has been solved.