I have to calculate the date or create a date which is 10 days into next month. I have with me month and year. If the month is April and year is 2019 I need a date which is 10th of may 2019.
You can simply use the methods available in the DateTime structure to do math with dates
// As example, replaces it with whatever date you have
DateTime current = new DateTime(2019,4,5);
DateTime next = new DateTime(current.Year, current.Month, 1).AddMonths(1).AddDays(9);
You can use AddMonths() and AddDays() extension method of DateTime.
From MSDN :
AddMonths() : Returns a new DateTime that adds the specified number of
months to the value of this instance.
AddDays() : Returns a new DateTime that adds the specified number of
days to the value of this instance.
//Considered this is your Current date
DateTime existingDate = new DateTime(2019, 4, 1);
//Below code will add +1 month to current month and +9 days to current date.
var result = existingDate.AddMonths(1).AddDays(9);
Output :
CurrentDate : 4/1/2019 12:00:00 AM
Next Date (+1 month and +9 days) :5/10/2019 12:00:00 AM
POC : .net Fiddle
var dateNow = DateTime.Now.AddMonths(1);
var date = new DateTime(dateNow.Year, dateNow.Month, 10);
Try this:
DateTime dt = new DateTime(2019, 04, 01);
DateTime newDT = dt.AddMonths(1).AddDays(9);
Related
I am given two dates as strings like this:
Beginning month: 10
Beginning year: 2010
Ending month: 01
Ending Year 2020
I want to query my entity and get everything that is equal or between these ranges.
So, I want everything from 10/2010 to 01/2020.
I have this code and I got stuck on how to convert the date correctly and the comparison:
dollartotals = (from x in se.AchBatches
where x.CompanyCode == company &&
DbFunctions.TruncateTime(x.DateTimeSubmitted) >=
// stuck here
select x.DollarTotal).Sum();
How do I handle the individual month/year strings and make a date comparison without a day?
Thanks for any assistance!
You want to check against the actual datetime submitted, not a truncated version of it.
The key is to build actual datetimes in advance, then just do a regular date window check.
Assume you have four strings as listed in your question:
//you might use TryConvert or a Try block here to validate your string data...
int beginYear = Integer.Convert(strBeginYear);
int beginMonth = Integer.Convert(strBeginMonth);
int endYear = Integer.Convert(strEndYear);
int endMonth = Integer.Convert(strEndMonth);
DateTime start = new DateTime(beginYear, beginMonth, 1);
DateTime endLimit = new DateTime(endYear, endMonth, 1).AddMonths(1);
dollartotals = (from x in se.AchBatches
where x.CompanyCode == company &&
x.DateTimeSubmitted >= start &&
x.DateTimeSubmitted < endLimit
select x.DollarTotal).Sum();
I want everything from 10/2010 to 01/2020.
Not sure if you want a DateTime sequence with every Tick between those dates, or every second, or every Day. Let's assume you want every Day: All Days from startDate.Date until and inclusive endDate.Date.
I use StartDate.Date, so if StartDate is 2020-02-05 13:20:14, then you still get February 5th 2020 at 00:00:00
IEnumerable<DateTime> GetDateRange(DateTime startDate, DateTime endDate)
{
DateTime lastDate = endDate.Date;
DateTime date = startDate.Date;
while (date <= lastDate)
{
yield return date;
date = date.AddDays(+1);
}
}
Usage:
var allDaysOfFebruary2020 = GetDateRange(new DateTime(2020, 01, 01),
new DateTime(2020, 02, 29));
You'll get the sequence from 1st February 2020 until and inclusive 29th February 2020.
I need a help to get the six-month range from the current date. For example, if the current date is 02-Feb-2016 then I would want start date 01-Sep-2015 and an end date 29-Feb-2016.
I have tried the code:
DateTime date = new DateTime(2016,02,06);
DateTime endDate = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
DateTime startDate = endDate.AddMonths(-6);
And Output is:
Startdate: 29-Aug-2015 endDate: 29-Feb-2016
But I want the following output:
Startdate: 01-Sep-2015 endDate: 29-Feb-2016
Update:
I have updated the question with an example which I had tried and output.
juharr's comment is the work for me.
Moving juharr's comment as an answer, as it is correct
Simply change:
DateTime startDate = endDate.AddMonths(-6);
to
DateTime startDate = new DateTime(date.Year, date.Month, 1).AddMonths(-5);
Based in the example in your question this should work for you:
DateTime end = new DateTime(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month, 1).AddDays(-1);
DateTime start = end.AddMonths(-6);
First of all, 180 days is not exactly six months. It looks like you need -3 months and +3 months. This will give you a six month breakout.
For months:
var dat = new DateTime(2016, 01, 01);
var startdat = dat.AddMonths(-3);
var enddat = dat.AddMonths(3);
however, if you really did mean 180 days....
For days:
var dat = new DateTime(2016, 01, 01);
var startdat = dat.AddDays(-90);
var enddat = dat.AddDays(90);
Now you have the two dates you need.
UPDATE 1:
if you just need -6 months, your function did work, but as I mentioned before, -180 days is not the same thing as -6 months.
You can still use the AddDays(-180) if you want 180 days, or AddMonths(-6) for 6 months back.
I am attempting to use the DateTime function in C# to calculate the last day of next month.
For example, today is December 17th 2015. I want the DateTime function to return January 31st 2016 (the last day of next month).
I am using the following to calculate the first day of next month (this works):
DateTime firstDayNextMonth = DateTime.Today.AddDays(-DateTime.Now.Day+1).AddMonths(1);
DateTime reference = DateTime.Now;
DateTime firstDayThisMonth = new DateTime(reference.Year, reference.Month, 1);
DateTime firstDayPlusTwoMonths = firstDayThisMonth.AddMonths(2);
DateTime lastDayNextMonth = firstDayPlusTwoMonths.AddDays(-1);
DateTime endOfLastDayNextMonth = firstDayPlusTwoMonths.AddTicks(-1);
Demo: http://rextester.com/AKDI52378
//system date or any date u want this case it is a calendar picker - 22/03/2016
DateTime today = dtpFrom.Value;
//Add a month to your date example , it now becomes - 22/04/2016
DateTime endOfMonth = new DateTime(today.Year, today.Month,today.Day).AddMonths(1);
//Get the last date off the above which is - 30
int getlastday = DateTime.DaysInMonth(endOfMonth.Year, endOfMonth.Month);
//Now set the date to the value which will be the last day off the next month - 30/04/2016
DateTime newDate = new DateTime(endOfMonth.Year, endOfMonth.Month, getlastday);
DateTime.DaysInMonth(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month);
var lastDayInNextMonth = DateTime.DaysInMonth(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month );
# Ben : DateTime.Now.AddMonths(1) will add 1 month to the current date not substract 11 months.
DateTime.Now.AddMonths(1).Year will give 2016 not 2015 refer the attached image
try this:
int Day= DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month+1>12 ? 01 : DateTime.Now.Month+1 );
I can get week number if I use normal way like that. As you know this one calculates week number according to normal start date which is 01.01.2015.
CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(mydate, CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
But I want to change that start date. For instance my first week of year will be 01.07.2015 and according to that date I want to calculate week of year for given date.
Substract a difference between new year and your start date from the mydate object
var startDate = new DateTime(2015, 7, 1);
var newYear = new DateTime(2015, 1, 1);
var culture = CultureInfo.CurrentCulture;
var weekOfYear = culture.Calendar.GetWeekOfYear(
mydate.Add(newYear - startDate),
culture.DateTimeFormat.CalendarWeekRule,
culture.DateTimeFormat.FirstDayOfWeek);
Maybe you could calculate the week number for your start date (e.g 01.07.2015 - 27) and then what is the week number for the actual date - e.g 12.12.2015 (50), and then just subtract - in this case 23?
Just subtract the number of days between your wished week-1 date and the default start date and use that offset each time you calculate (.AddDays(offset)).
That way :
DateTime startDateTime = new DateTime(2015,07,01) ;
int fisrtDayOfWeek = 0 ; // 0:sunday for US, 1:Monday for many other coutries
DateTime week1StartDateTime = startDateTime ;
for (int i=1;i<6;i++) if ((int)startDateTime.AddDays(i).Day==fisrtDayOfWeek )
week1StartDateTime = startDateTime.AddDays(i) ;
int weekNumber= mydate<week1StartDateTime ? 1 :
((int)(mydate-week1StartDateTime).TotalDays)/7+1 ;
// note : casting double to int provides the floor (not round)
I want to return the name of day like Saturday or Monday
I used this code :
DateTime date = new DateTime(DateTime.Now.Date.Day);
MessageBox.Show(date.DayOfWeek.ToString());
But it doesn't work it return the name of day but doesn't correct day
and when i change the date in my computer it still return the same day
Rather try something like
MessageBox.Show(DateTime.Today.DayOfWeek.ToString());
DateTime.Today Property
Gets the current date.
Your problem is that
DateTime date = new DateTime(DateTime.Now.Date.Day);
evaluates to
{01/Jan/0001 12:00:00 AM}
The constructor you used was DateTime Constructor (Int64)
Initializes a new instance of the DateTime structure to a specified
number of ticks.
This line:
DateTime date = new DateTime(DateTime.Now.Date.Day)
Should be:
DateTime date = new DateTime(DateTime.Now)
You are putting the day in a date variable which will probably be in the year 1900.