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

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

+5
source share
4 answers

Suppose 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.TotalDayscontains the number of days (thanks @Tung). lastdatealso contains the last date of the year.

+18
source

gotta do the trick

int daysLeft = new DateTime(DateTime.Now.Year, 12, 31).DayOfYear - DateTime.Now.DayOfYear;

+2
source

new DateTime(suppliedDate.Year, 12, 31).Subtract(suppliedDate).TotalDays

+1
source

I think you should try TimeSpan, for example

 DateTime startTime = DateTime.Now;

 DateTime endTime = DateTime.Now.AddSeconds( 75 );

 TimeSpan span = endTime.Subtract ( startTime );
 Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
 Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
 Console.WriteLine( "Time Difference (hours): " + span.Hours );
 Console.WriteLine( "Time Difference (days): " + span.Days );
0
source

All Articles