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..
我想获得从给定日期开始一年内剩余的总天数。假设用户是否给出了04-01-2011(MM-DD-YYYY),我想找到剩下的剩余天数。这个怎么做..
4 个解决方案
#1
20
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.
diff.TotalDays包含天数(感谢@Tung)。 lastdate还包含一年的最后日期。
#2
2
should do the trick
应该做的伎俩
int daysLeft = new DateTime(DateTime.Now.Year, 12, 31).DayOfYear - DateTime.Now.DayOfYear;
int daysLeft = new DateTime(DateTime.Now.Year,12,31)。DayOfYear - DateTime.Now.DayOfYear;
#3
1
new DateTime(suppliedDate.Year, 12, 31).Subtract(suppliedDate).TotalDays
new DateTime(suppliedDate.Year,12,31).Subtract(suppliedDate).TotalDays
#4
0
I think you should try TimeSpan like
我想你应该试试TimeSpan
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 );
#1
20
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.
diff.TotalDays包含天数(感谢@Tung)。 lastdate还包含一年的最后日期。
#2
2
should do the trick
应该做的伎俩
int daysLeft = new DateTime(DateTime.Now.Year, 12, 31).DayOfYear - DateTime.Now.DayOfYear;
int daysLeft = new DateTime(DateTime.Now.Year,12,31)。DayOfYear - DateTime.Now.DayOfYear;
#3
1
new DateTime(suppliedDate.Year, 12, 31).Subtract(suppliedDate).TotalDays
new DateTime(suppliedDate.Year,12,31).Subtract(suppliedDate).TotalDays
#4
0
I think you should try TimeSpan like
我想你应该试试TimeSpan
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 );