This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 9 years ago.
I want to calculate no of days remaining for particular day from today onwards. How to calculate difference between 2 dates in C#??
(Note: This answer was written when the question was worded very differently)
Assuming your dates are DateTime or DateTimeOffset:
TimeSpan delta = date1 - date2
TimeSpan GetDelta(DateTime d1, DateTime d2)
{
return (d1 - d2).TotalDays;
}
Related
This question already has answers here:
Why DateTime.AddHours doesn't seem to work?
(8 answers)
Closed 2 years ago.
This is my code, and I don't understand why it doesn't work. This is literal copy pasta [edit] from my own code base; there should be no hidden complexities [edit] from elsewhere in the code. This question/answer implies that this should work. What am I missing?
DateTime billDate = new DateTime(2020, 3, 1); // March 1st, 2020
MessageBox.Show($"billdate month {billDate.Month}"); // billdate month 3
billDate.AddMonths(-1); // subtract a month
MessageBox.Show($"month after change {billDate.Month}"); // month after change 3
I expect the month to decrease to 2. Why doesn't it?
AddMonths returns a new DateTime object; it doesn't mutate the existing one. Try:
billDate = billDate.AddMonths(-1);
This question already has an answer here:
DateTime losing Milliseconds
(1 answer)
Closed 5 years ago.
DateTime x = DateTime.ParseExact("2017_10_16 13:52:03.112","yyyy_MM_dd HH:mm:ss.fff", null);
Console.WriteLine(x);
"10/16/2017 1:52:03 PM"
It gives output as "10/16/2017 1:52:03 PM"
Here I am missing the milliseconds 112.
I have looked into many online posts but it does not work for me.
What you have is correct. Only Console.WriteLine(x); does not show you milliseconds by default.
If you ask for this: Console.WriteLine(x.ToString("fff")); you will see your milliseconds
This question already has answers here:
Can you round a .NET TimeSpan object?
(10 answers)
Closed 6 years ago.
Round timespan seconds I also need do same with .Hours and .Minutes
This:
1.53994 second
to:
1 second
This code worked for me.
TimeSpan ts = TimeSpan.FromSeconds(1.54);
Math.Round(ts.TotalSeconds);
This question already has answers here:
To get the mondays to saturdays in the current month
(3 answers)
Closed 8 years ago.
How do I get the date of all sunday and saturday in a particular year and count the result in C#.
I've searched a lot but only in java gives me result how abut in C#?
I don't know if you already have some code, but you should check DateTime.DayOfWeek.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I calculate someone’s age
How can i calculate age using datetimepicker in c#?
Strictly speaking,
TimeSpan age = DateTime.Now - dateTimePicker.Value;
However, figuring out someone's "age" is only slightly more complicated.
int years = DateTime.Now.Year - dateTimePicker.Value.Year;
if(dateTimePicker.Value.AddYears(years) > DateTime.Now) years--;
Because years vary in length you'll have to do this rather than relying on a structure like the TimeSpan that represents a specific amount of time (the same is true for figuring out how many "months" are between two dates, since months vary in length from 28-31 days).
The last line of code is there to account for the person's birthday not yet taking place this year.
Assuming that the DateTimePicker is called dtpBirthday:
int age = DateTime.Now.Year - dtpBirthday.Value.Year - (DateTime.Now.DayOfYear < dtpBirthday.Value.DayOfYear ? 1 : 0);