C# date rolling 13 months values in global variable .NET - c#

In C# how do I get the below datetime values in .NET. I am trying to look at todays date and trying to get a rolling 13 months. So last month minus 13 months which is december 2017 but need to get first day of the month at 00:00:00.000. Also trying to get an enddate as below which is end of last month.
Trying to get this and assign it to a variable in my code.
StartDate:
2017-12-01 00:00:00.000
EndDate:
2019-01-31 23:59:59.000
Whats the best way to get this?

Here you go
DateTime mydate = DateTime.Today;
//2017-12-01 00:00:00.000
DateTime StartDate = new DateTime(mydate.Year, mydate.Month, 1).AddMonths(-14);
//2019-01-31 23:59:59.000
DateTime EndDate = new DateTime(mydate.Year, mydate.Month, 1).AddSeconds(-1);

You can get the date with DateTime date = DateTime.Today.AddMonths(-14).AddDays(-(DateTime.Today.Day - 1));
.AddMonths(-13) yielded january 2018 for me

Related

Calculating for remaining days in current month [duplicate]

This question already has answers here:
How to find the first day of next month,if the current month is december
(12 answers)
Calculate difference between two dates (number of days)?
(17 answers)
Closed 5 years ago.
I would like to know how to calculate the remaining days of the month e.g (15 Feb 2017 to 28 Feb 2017) without the use of a datetimepicker set at 28 Feb 2017.
Here are my codes for subtraction of 2 datetimepicker:
DateTime startDate =
(DateTime)dateTimePicker2.Value;
DateTime endDate =
(DateTime)dateTimePicker1.Value;
TimeSpan ts = endDate.Subtract(startDate);
textBox10.Text = ts.Days.ToString();`
Here are the steps you need to go through:
take your current date
use DateTime.AddMonths() to generate a new date one month from your current date
create a new date that uses 1 for the day, and the month and year from the future date you just worked out
subtract your current date from the future date, this will give you a Timespan which contains the number of days difference
You can use the closely related question Calculate difference between two dates (number of days)? as a guide.
Here is an example, but you can use the Value property from your DateTimePicker instead. DateTime.DaysInMonth(int year, int month) is a helpful method.
DateTime beginDate = new DateTime(2017, 2, 15);
var daysLeft = DateTime.DaysInMonth(beginDate.Year, beginDate.Month) - beginDate.Day;
Console.WriteLine("days from Feb 15 to Feb 28: {0}", daysLeft);
Output:
days from Feb 15 to Feb 28: 13
You can try:
DateTime dt = DateTime.Now;
int days = dt.AddDays(1 - dt.Day).AddMonths(1).AddDays(-1).Day - dt.Day;

c# adding correct month based on previous month

I am trying to add a month to a date and populate textboxes based on the previous month. For months that end on the 31st (and even February 28th) this works. But, if the previous month ended on the 30th and the next month ends on the 31st, it is either one day short or one day long. For example:
Previous start date: 4/1/2017
Previous end date: 4/30/2017
New start date: 5/1/2017
New end date: SHOULD BE 5/31/2017
Here is the code I have:
// Set the Start Date
DateTime dtNewStartDate;
dtNewStartDate = Convert.ToDateTime(txtRecentBeginDate.Text).AddMonths(1);
txtNewStartDate.Text = dtNewStartDate.ToString();
// Set the End Date
DateTime dtNewEndDate;
dtNewEndDate = Convert.ToDateTime(txtNewStartDate.Text).AddMonths(1);
txtNewEndDate.Text = dtNewEndDate.ToString();
This produces an end date of 6/1/2017 instead of 5/31/2017
EDIT: I was able to find what I was looking for from https://social.msdn.microsoft.com/Forums/en-US/218260ec-b610-4fa6-9d1b-f56f3438b721/how-to-get-the-last-day-of-a-particular-month?forum=Vsexpressvcs.
This solution accounts for leap years and getting the correct last day of the month for any circumstance. This is what I came up with:
// Set the End Date
int intYear = dtNewStartDate.Year;
int intMonth = dtNewStartDate.Month;
int intNumberOfDays = DateTime.DaysInMonth(intYear, intMonth);
DateTime dtNewEndDate = new DateTime(intYear, intMonth, intNumberOfDays);
txtNewEndDate.Text = dtNewEndDate.ToString();
You can just get the last day of the month for that month.
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year,
today.Month,
DateTime.DaysInMonth(today.Year,
today.Month));
This is what I typically do: Just take the year and month from the start date, create a new date based on that, add a month and subtract a day which ends up being the last day of the next month::
DateTime dtNewEndDate = new DateTime(dtNewStartDate.Year, dtNewStartDate.Month, 1)
.AddMonths(1)
.AddDays(-1);

Calendar set maxDate to certain day?

I'm creating a datetimepicker and I want it to have a maxDate until Saturday every week.
So for example, today is Sunday 16/10/2016, the maximum for this is 22/10/2016. But if i put maxdate = today + 7 , tuesday maxdate will be monday which I don't want.
I want to allow the user to be able to choose in 1 week only.
Is this possible ?
Edit:
Sorry,I wrote the wrong thing. It is calendar, not datetimepicker.
DateTime has a DayOfWeek property (that will output the DayOfWeek Monday if it is Monday). You could pass that property into a helper method that determines the number of days until Saturday based on the current day...
You could try this:
DateTime today = DateTime.Today;
int daysToAdd = 6 - (int)today.DayOfWeek;
DateTime nextSaturday = today.AddDays(daysToAdd);
Console.WriteLine(nextSaturday.ToShortDateString());
Ouputs:
10/22/2016

Generate Date Time offset

I have DateTimeOffset input param. Need to create other DateTimeOffset param which Month is 2 month less than from input:
//DateTimeOffset input;
DateTimeOffset modified = new DateTimeOffset(input.Year,
input.Month - 2, input.Day,
input.Hour, input.Minute,
input.Second, input.Millisecond,
input.Offset);
I get an exception
Year, Month, and Day parameters describe an un-representable DateTime.
What is wrong? - the month is 4. S0 4-2 = 2 is valid
Thanks
I think the month is a red herring, it's probably more likely your day is above 28 which means you are trying to create a date like 30th Feb (which doesn't exist!).
Use the AddMonths method instead
DateTimeOffset modified = input.AddMonths(-2);

Calculate End Date Given Start Date & duration (in days)

I am developing a c#.net solution with a Store Procedure in oracle that calculates End Date based on provided Start date and Duration with weekends.
i.e. Start Date: 01/3/2013 Forward days: 10
Start Date = (05/5/2011) - 10 days (includes weekends)
Start Date = 06/02/2013
thank you,
Use of AddDays method of DateTime
DateTime startDate = new DateTime(2013,3,1);
var endDate = startDate.AddDays(10);
You can do this a bunch of ways in Oracle. I'd suggest the INTERVAL way because it's standard-ish:
SELECT yourdate + INTERVAL '10' DAY, ...
The single quote around the 10 is required.
The "old" way is to just add 10 to the date; Oracle treats this as days:
SELECT yourdate + 10, ...
//DateTime start_date = ...
DateTime end_date = start_date.AddDays(10);

Categories