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

后端 未结 5 1045
别跟我提以往
别跟我提以往 2021-01-05 00:24

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 remain

5条回答
  •  温柔的废话
    2021-01-05 00:52

    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.

提交回复
热议问题