Search code examples
c#asp.net

How to get the total number of days in a year from the given date


I would like to get the total number of days in a year left from the given date .. Assume if a user gives 04-01-2011(MM-DD-YYYY) I would like to find the remaining days left. How to do this..


Solution

  • Let's say the date is today:

    var user = "05-08-2012";
    var date = DateTime.ParseExact(user, "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture);
    var lastdate = new DateTime(date.Year, 12, 31);
    var diff = lastdate - date;
    

    diff.TotalDays contains the number of days (thanks @Tung). lastdate also contains the last date for the year.