简体   繁体   中英

how to get no of days and nights between two dates using c#

i am doing a project on cab services.in this rate is different for day and night. in the form only journey start date and end date is selected.based on this i have to calculate the no of days and nights.

here i am confused how to calculate the no of days and night.

thanks in advance.

private List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
{
    if (StartingDate > EndingDate)
    {
        return null;
    }
    List<DateTime> rv = new List<DateTime>();
    DateTime tmpDate = StartingDate;
    do
    {
        rv.Add(tmpDate);
        tmpDate = tmpDate.AddDays(1);
    } while (tmpDate <= EndingDate);
    return rv;
}  

To view this code in action, copy and paste the following code into SnippetCompiler:

DateTime StartingDate = DateTime.Parse("02/25/2007");
DateTime EndingDate = DateTime.Parse("03/06/2007");
foreach (DateTime date in GetDateRange(StartingDate,EndingDate))
{
   WL(date.ToShortDateString()); 
} 

Sample output :

2/25/2007
2/26/2007
2/27/2007
2/28/2007
3/1/2007
3/2/2007
3/3/2007
3/4/2007
3/5/2007
3/6/2007

Use the Subtract method to get the difference, which is a TimeSpan value. Example:

TimeSpan diff = SecondDate.Subtract(FirstDate);

You can get the length of the time span for example in hours:

double hours = diff.TotalHours;

I'm not sure which time unit "days and nights" could be interpreted as, though. Perhaps days?

double days = diff.TotalDays;
DateTime dt1,dt2;
//...
TimeSpan period = dt1 - dt2;
int days = period.Days;

It sounds like a very long Cab journey that takes days and nights!

I think you need to define what a day and a night is more clearly in order to get your perfect answer. You also need to think about what impact Daylight Saving Time has on your calculations.

If say:

  • a day was the period from 6am to 6pm
  • the night was the rest - from 6pm to 6am
  • and you wanted to really count hours rather than days

In this case then a calculation would require you to:

  • iterate a currentDateTime from the startDateTime to the endDateTime
  • choose the increment in the currentDateTime so that it jumps to the next time barrier (6am, 6pm or the endDateTime)
  • within each loop, then add to your cumulative calculation of numDayHours or numNightHours so far.

Note that:

  • you could make this calculation quicker by counting whole days along the way
  • you need to be very careful about the time zone you are calculating in (I just hope that your taxi doesn't cross time zone boundaries!)
  • you need to be very careful about local time changes - especially "daylight savings time" type changes - the duration from 6pm to 6am is not always 12 hours !

Some pseudo code:

   var numDayHours = 0.0;
   var numNightHours = 0.0;

   var current = startDateTime;

   while (current < endDateTime)
   {
       next_hop = calculate_next_hop (current, endDateTime);

       // select next date time
       switch (next_hop.hop_type)
       {
           case HopType.night_time_hop:
               numNightHours += next_hop.num_hours;
               break;

           case HopType.day_time_hop:
               numDayHours += next_hop.num_hours;
               break;
       }

       current = next_hop.EndDateTime;          
   }

   // and here is the result
   double numDays = numDayHours / 12.0;
   double numHours = numNightHours / 12.0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM