I need help to creating a function that can divide a period of time into regular periodicity of year.
For example: I have a period from 11/10/2011 to 08/07/2012 divided on regular semester. I want to get this result in a list:
1- 11/10/2011 - 12/31/2011
2- 01/01/2012 - 06/30/2012
3- 07/01/2012 - 08/07/2012
As you said, it is little bit complex but not impossible. With small thought you might get this easily. I've implemented code but to give your brain some work I'm not posting the code but giving you pseudo code below.
I thought of following way. First you need to have a duration through which you want to divide a year, for ex: 6 months.
Take the start date and create a new date by using DateTime constructor (year, month, day) and pass the year in the start date as year and month and day as 1 as below:
new DateTime(startdate.Year, 1, 1);
This will give you start date of that year. Then add duration days/months to that date to get the next periodicity. If your start date is less than this new date then add once again duration to that new date till you get the date greater than start date.
With the above logic you can form the periods you want till the end date. Of course you have to check whether your end date as well against this periodicity. There are many other conditions you need to check to get the proper system to give these periods for any given start and end dates.
Related
I created a pipeline in data factory and I want to retrieve data from a source for the current month and for the previous month. When I run the pipeline I give the needed parameter named ExtractDate. The format is MM/DD/YYYY .
For the current month I used the following expression in 'Set Variable' activity:
#replace(item().Query,'EXTRACTDATE',formatDateTime(variables('ExtractDate'), 'yyyyMM'))
And for the previous month I tried:
#adddays(variables('ExtractDate'),-28)
The problem appears when the user will set when running the pipeline the date 07/31/2019 for example. Then the previous month will still be July. And if I increase the number to 31, then there is a possibility that the user will introduce 03/01/2019 and from March it will skip the month of February.
I tried to think of a solution, but unfortunately there is no 'addmonths' available in data factory.
Any ideas please?...I've spent 2 days on this issue..
addMonths and addYears are not supported by ADF so far.Please vote up this thread to push the progress.
My trick is use combination of bulit-in functions in ADF. Please see my test:
This month is very simple:
#concat(substring('07/16/2019',6,4),substring('07/16/2019',0,2))
output:
Last month is little complex.It should check if it is the first month of the year.
#if(equals(substring('07/16/2019',0,2),'01'),
concat(
string(sub(
int(substring('07/16/2019',6,4)),1)),
'12'),
string(sub(
int(concat(substring('07/16/2019',6,4),
substring('07/16/2019',0,2))),1)
)
)
if the input param is 01/16/2019,then the output looks like:
My test is based on the static value,please replace it with your variable.
Just for summarize:
The final working dynamic content should be like as below:
#if( equals(variables('SubstringMonth'),'01'),
concat(string(sub(int(variables('SubstringYear')),1)),'12'),
concat(variables('SubstringYear'),string(if(or(equals(sub(int(variables('SubstringMonth')),1),11),equals(sub(int(variables('SubstringMonth')),1),10)),
sub(int(variables('SubstringMonth')),1) ,
concat('0',string(sub(int(variables('SubstringMonth')),1) )))) ))
For the previous month from today you could use
formatDateTime(AddToTime(utcnow(), -1, 'Month'), 'yyyy-MM-dd')
I am making a loop where part of it will be creating new records and another part - should increment date field by one month without affecting the days, for every new record loop.
For example, default date is 2016/07/19 - dates for new records should go as follow - Record 1 - 2016/08/19; Record 2 - 2016/09/19; Record 3 - 2016/10/19
Here's a snippet of my code
startDate = (DateTime)target["xrm_startDate "];
while(a>b)
{
startDate = startDate.AddMonth(1);
}
With this loop, date is being increased by one month, however days outputted are different, depending on how many days month has.
There is no standard DateTime.AddMonth method. The DateTime.AddMonths method returns a date a given number of months later, with the day-part corrected if that day is not valid:
If the resulting day is not a valid day in the resulting month, the
last valid day of the resulting month is used. For example, March 31st
+ 1 month = April 30th
Are you looking for?
startDate = new DateTime(startDate.Day, startDate.Addmonths(1).Month, startDate.Year)
Also please check for the year.
I am using DateTime.Today.
Now I'm not sure if the date is from the beginning of the day or the end of the day.
This is what DateTime.Today returns : {11-3-2014 0:00:00}
MSDN states the following: "An object that is set to today's date, with the time component set to 00:00:00."
This means that a DateTime object is created with today's date at the absolute start of the day hence 00:00:00.
You can check if it is the start of the day by using the AddHour() method of the DateTime class.
DateTime d = DateTime.Today;
//AddHours, AddMinutes or AddSeconds
d = d.AddHours(1);
if (d.Date != DateTime.Today.Date)
{
//Not the same day
}
If d.date should be different the date was initialised at a different time (eg. 23:00:01).
http://msdn.microsoft.com/en-us/library/system.datetime.today(v=vs.110).aspx
0:00:00 is the start of the day and 23:59:59 is the end of day.
You can also confirm through this 24-hour clock
In the 24-hour time notation, the day begins at midnight, 00:00, and the last minute of the day begins at 23:59. Where convenient, the
notation 24:00 may also be used to refer to midnight at the end of a
given date[5] – that is, 24:00 of one day is the same time as 00:00 of
the following day.
On a side note:-
If you want to know the time then use .Now because that includes the 10:32:32 or whatever time; however .Today is the date-part only (at 00:00:00 on that day ie the begining of the day). So you can say that .Today is essentially the same as .Now.Date
Thats the beginning of the day, the end of the day would be:
{11-3-2014 23:59:59}
And remember, the only stupid question is the one you don't ask :)
DateTime.Today returns the current DateTime value, without the Time part.
Which means, it is the the first possible DateTime value for the current day.
Think of it like this:
The last moment in a day can be 23:59 or theoretically any amount of nanseconds before the next day. the next day then starts at 00:00:00 counting upwards.
So 11-3-2014 0:00:00 marks the beginning of a day. Either the earliest possible moment, or no time at all, if you want to treat 0:00:00 as a default value.
I'm writing a search program that includes a date range- DateFrom and DateTo, both of which are DateTimes. Searching January - April for any search criteria will return the results of January + February + March + April.
I would like to add functionality whereby a user can choose to search each month within a - range, so searching January - April will return each individual month's results. However I'm having trouble finding an intelligent way to implement this for any unit of time larger than days.
So far I'm getting a TimeSpan using:
TimeSpan ts = query.DateTo - query.DateFrom;
In a perfect world I'd just be able to do something like foreach (month m in TimeSpan){dostuff}, however TimeSpan stores dates as integers and does not include any units larger than days. Additionally, I thought maybe I could just use n = DateFrom.month - DateTo.month to get the difference in months and run a function in a for loop starting with DateFrom and lasting n months, but this won't work between years.
The last case is definitely fixable but includes a number of tedious special cases. Is there a cleaner / more elegant way of accomplishing this sort of iteration that I'm missing?
Thanks.
So for the basic pattern we can use a fairly simple for loop:
public static IEnumerable<DateTime> Months(DateTime start, DateTime end)
{
for (DateTime date = start; date < end; date = date.AddMonths(1))
yield return date;
}
Now in this case we have a start date that is inclusive and an end date that is exclusive. If we want to make the end date inclusive, as you have described, we can add:
end = end.AddMonths(1);
to the start of the method.
Next you have a few other considerations. Are the datetime objects passed in going to always be the first of the month? If not, how do you want to support it? If the start date is Feb 10th do you want the first yielded date to be Feb 1st (the start of the start date's month), March 1st (the first "first day of the month" on or after the start date), or Feb 10th (meaning that each date in the timespan would be the 10th day of that month)?
Those same questions also apply to the end date; do you want the last "first day of the month" before the end date, the first day of the next month, etc.
Also, what should happen if the start date is after the end date? Should it yield the dates "backwards", should it just pretend the start date is the end date and the end date is the start date? Should it keep adding days until you've overflowed DateTime and come back around to that date?
Pretty much all of these issues aren't too hard to deal with, the hard part is just knowing what you want to do in each case.
You could do something like:
var months = ((query.DateTo.Year - query.DateFrom.Year) * 12) + query.DateTo.Month - query.DateFrom.Month
for(int i=0;i<months;i++){
//do stuff as below
var currentDate=query.DateFrom.AddMonths(i);
}
I am trying to get the number of days between two datetimes, but not according to the exact timespan, rather according to the "day date" difference between date a and date b. No hours taken in account.
So far, I calculated the age of an item using :
(DateTime.Now - creationDate).Days
The problem with this code is that, for something that was created the day before but less than 24h ago, it will output "0 days", which is not very clear to my users apparently.
So what I want to accomplish is, even for an item that was created at 11:59pm for example, to get an output of "1 day old" as soon as the clock hits midnight. How could I accomplish this?
Two options:
1) Take the two dates first:
var days = (DateTime.Today - creationDate.Date).Days;
2) Use my Noda Time date/time library instead, using LocalDate for the two dates involved, and then Period.Between(start, end, PeriodUnits.Days) to get a period of just days. (You can also get weeks, months etc.) Admittedly getting "today's date" is deliberately a bit trickier in Noda Time - where .NET implicitly uses "the system time zone," Noda Time forces you to be more explicit.
I would use it simply like
int days =(int)DateTime.Now.Subtract(creationDate).TotalDays;
hope this helps