How can I get the total days as int between DateTime.Now and a specific DateTime? Can this be done using LINQ?
You can't create a DateTime without any value (ie, null). It will always have a default value ( DateTime. MinValue ). A DateTime? , on the other hand, is a sort of wrapper around DateTime , which allows you to keep it undefined.
The DaysInMonth method always interprets month and year as the month and year of the Gregorian calendar even if the Gregorian calendar is not the current culture's current calendar. To get the number of days in a specified month of a particular calendar, call that calendar's GetDaysInMonth method.
Use DateTime. Subtract to get the difference between two dates in C#.
You can simply calculate the difference using the following method:
DateTime a = ;//some datetime
DateTime now = DateTime.Now;
TimeSpan ts = now-a;
int days = Math.Abs(ts.Days);
Simply subtract two DateTime
s from each other, into a TimeSpan
, and get the TotalDays
component from it.
TimeSpan diff = DateTime.Now - OtherDateTime
int days = (int)Math.Abs(Math.Round(diff.TotalDays));
It is rounded to remove the fraction caused by the hours/minutes/seconds, and the absolute value is given so you get the exact change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With