6
votes

Obtenez le nombre de jours entre deux objets DateTime différents

Comment puis-je obtenir la différence de jours entre deux objets DateTime? XXX

J'ai besoin d'avoir la différence entre initialeDate et enddate.


0 commentaires

5 Réponses :


11
votes

Utilisez la méthode de soustraction:

static void Main(string[] args)
{
    DateTime begin = DateTime.Now;
    DateTime end = begin.AddYears(1).AddMonths(1);
    var result = end.Subtract(begin).TotalDays;
}


0 commentaires

1
votes
DateTime start = DateTime.Now;
DateTime end = DateTime.Now.AddDays(5);
TimeSpan span = end.Subtract(start);
return span.Days;

0 commentaires

3
votes
DateTime dt1 = new DateTime(2011,01,01);
DateTime dt2 = new DateTime(2011,01,20);
int ireturn = (int)dt2.Subtract(dt1).TotalDays;

0 commentaires

1
votes
        int days = 10;
        DateTime initialDate = DateTime.Now;
        DateTime endDate = DateTime.Now.AddDays(days);
        TimeSpan duration = endDate.Subtract(initialDate);
In this example you can use either span.Days or span.TotalDays, however you have to be very careful with TimeSpan properties.  If you look at the TotalHours vs Hours for example you'll see that they are not the same.The Hours property is the number of hours remaining after the days property has been taken off (in this case zero), the Total hours is the TimeSpan represented in hours.

0 commentaires

2
votes

Soustraire DateTimes donnera une période de temps: xxx


0 commentaires