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
Perhaps just:
DateTime.IsLeapYear(DateTime.Now.Year) ? 366 : 365
Sorry, read it as if you just wanted the number of days in current year...
I think you should try TimeSpan like
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 );
should do the trick
int daysLeft = new DateTime(DateTime.Now.Year, 12, 31).DayOfYear - DateTime.Now.DayOfYear;
new DateTime(suppliedDate.Year, 12, 31).Subtract(suppliedDate).TotalDays
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.