Subtracting DateTime Months using .AddMonths(-1) does nothing? [duplicate] - c#

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);

Related

How can I add 1 minute to current time that I get? webdriver c# [duplicate]

This question already has answers here:
Add hours or minutes to the current time
(4 answers)
Closed 7 years ago.
I get the current time and need to add 1 minute to it so It would be 1 minute ahead, how do I do that?
string date1 = System.DateTime.Now.ToString("HH:mm");
All you have to use is DateTime.AddMinutes
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
Conversely, if you want to subtract a minute just use a negative parameter.
DateTime.AddMinutes method can do that.
If you want a string representation then do:
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
As a side note, you can also supply a negative number to subtract minutes from current time like:
DateTime.Now.AddMinutes(-1)

add the specified number of days to current date in c# [duplicate]

This question already has answers here:
Add 1 week to current date
(3 answers)
Closed 8 years ago.
I want to add number of days with the current date. Help me to find a proper solution. Thank you.
Code:
string s = DateTime.Now.ToString();
I want to add 2 days with the current date.
string s = DateTime.Now.AddDays(2).ToString();
UPDATE
In answer to your comment
string s = DateTime.Now.AddDays(2).ToShortDateString();
DateTime.Now.AddDays(2).ToString();
You can use the AddDays method on the DateTime struct. You can use that method to add or substract any amount of days from the current date, like in this sample.
DateTime added = DateTime.Now.AddDays(2);
string s = added.ToString();
Or even:
DateTime substracted = DateTime.Now.AddDays(-2);
Note that if you are only interested in the date, you could use DateTime.Today rather than DateTime.Now since that will be a little more performant.
A final note on the use of ToString: the output of ToString may differ when the OS running on uses different cultures. If you intend to process this outputted string later on, I suggest to pass in the desired culture, using this overload of ToString.

getting date of all SATURDAY and SUNDAY in a particular year in C#? [duplicate]

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.

Calculate no of days remaining [duplicate]

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;
}

Subtract one day from DateTime object [duplicate]

This question already has answers here:
Is there any easy way to increment a DateTime by monthly/yearly/daily units without having to parse it out like crazy?
(4 answers)
Closed 7 years ago.
I want to substrate one day from my date, so that when my date is 30-7-2013 than I want 29-7-2013
Just use the AddDays method, remembering that it doesn't change the value it's called on - it returns a new DateTime value.
DateTime date = ...;
date = date.AddDays(-1);
I am not familiar with windows phone development but you may try to use DateTime.AddDays(-1) method call. According to MSDN this call is supported by windows phone OS.
For example:
var today = DateTime.Now;
var yesterday = today.AddDays(-1);
Alternative version:
DateTime date = ...;
date -= TimeSpan.FromDays(1);

Categories