Subtract number of days based on provided date parameter - c#

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

Related

How to get date after 12 months in c#

I have been given a task to get future date. I mean if today is 1/1/2016 (1st Jan,2016) and when i add 12 months to it then normally it gives 1/1/2017 if i do like this code :
dateTimeObj="1/1/2016"
string futureDate=dateTimeObj.AddMonth(12);
Now this future date will give 1/1/2017 using this code but i have been asked to get when we add 12 months then it must give 12/31/2016 (31 dec,2016) (not 1/1/2017)
How to achieve this ? Is there any inbuilt function to do this, If not then how to do it programtically?
Note: The software on which i am coding is for accounts, they need the date this way only.
I think you should do this
dateTimeObj.AddYears(1).AddDays(-1);
I think there is no special Feature. Just so something like this:
date.AddMonths(12).AddDays(-1);
best regards
When adding 12 months to today, the expected result is same date in next year. So what you have to do is subtract a timespan of one day from it. You can try the above methods in the comments or you can follow this.
This is giving the expected result as 12-31-2016
var dateTimeString = "1/1/2016";
DateTime dateTimeObj = DateTime.Parse(dateTimeString);
DateTime futureDate = dateTimeObj.AddMonths(12).Subtract(TimeSpan.FromDays(1));
Console.WriteLine(futureDate.ToString("MM/dd/yyyy"));
Console.ReadLine();

Get week number and year as YYYYWW from current date (SSIS script task) plus N weeks in the future

I have seen this answer which explains how I might get the relevant week number.
Does anyone know how I could change the format to Year and Week like this YYYYWW i.e. 201317.
Based on it being week 17, how could I get the correct Year and Week in N weeks time? i.e. 60 weeks from the current week including the current week? but as YYYYWW with the correct Year and Week number in that given year?
I am trying to do this in a script component of an SSIS package 2008 which supports up to .NET 3.5.
I have also tried referencing NodaTime in a Script Task inside the package and I can reference it fine and build the script, but when it comes to executing the task just on my machine, I get a FileNotFound Exception for NodaTime.dll.
Any help trying to achieve this just using .NET 3.5 and C# would be appreciated.
UPDATE: Here is an example of some of the week data I have recently been given. I didn't know my exact requirement and I still don't really know. This looks to me like the weeks start on a Sunday and that there is no leap week at the end of this year. I can only guess at whether this is the Gregorian calendar or a modification of it.
There's a very simple method to calculate the current week using System.Globalization.CultureInfo:
using System.Globalization;
CalendarWeekRule weekRule = CalendarWeekRule.FirstFourDayWeek;
DayOfWeek firstWeekDay = DayOfWeek.Monday;
Calendar calendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;
int currentWeek = calendar.GetWeekOfYear(DateTime.Now, weekRule, firstWeekDay)
The above method will almost (see Jon Skeet's linked article) return the ISO 8601 week date; this is the standard used by many governments and industries (including import/export, and multi-national companies).
In order to get the desired format of YYYYWW, simply use:
String myYYYYWW = String.Format("{0:0000}{1:00}", DateTime.Now.Year, currentWeek);
To obtain N weeks in the future, you can let the DateTime class do the calculation for you...
DateTime myFutureWeek = DateTime.Now.AddDays(N * 7); // add N weeks
// ... steps outlined above ...
int futureWeek = calendar.GetWeekOfYear(myFutureWeek, weekRule, firstWeekDay)
If you'd like to deviate from the almost-ISO week number, simply change the weekRule and firstWeekDay variables. You may also want to play around with the calendar of specific cultures.
The Wikipedia article on ISO week date, contains a section for other week numbering systems, such as that used in the United States.

Getting the closest weekday to today in from a matrix (weekdays in ddd format)

I've been given a bit of a head ache when I was asked to see if i could get a simple solution to a problem at work.
This is the current situation:
We have a date (Mostly todays date) from which I can easily get the Weekday parameter using
dEarliest.ToString("ddd");
In a database we have a register of which day a certain task is to be performed with the weekdays stored as "ddd" format (i.e Mon, Tue, Wed, Thu etc).
And, the problem, How can I get which day in the database records that are the closest to today?
I've been sitting here trying to figure it out with some "smart" SQL query, but I just don't know how to wrap my head around it.
I was thinking enum:s, but I'm not sure if that'd work, and if it would, how? I'm quite new at c#
hence the, probably, stupid question.
Any help would be greatly appreciated. :)
The best way to do this is to make a comparrison between the DateTime's DayOfWeek value (the property DayOfWeek is of type enum DayOfWeek) and the output of SQL Server's DATEPART function.
For example, if you wanted to get all the tasks for yesterday, you might do this in C#:
int yesterday = ((int)(DateTime.Now.DayOfWeek))-1;
and then pass that value into a SQL statement that does this:
SELECT * FROM <table> WHERE DATEPART(weekday, <column_name>) = (#yesterday+1)
The #yesterday+1 part is because C#'s enum uses 0-6 for the days while SQL Server uses 1-7 (both uses Sunday as the first index).

Add 1 week to current date

I've got something like this DateTime.Now.ToString("dd.MM.yy"); In my code, And I need to add 1 week to it, like 5.4.2012 to become 12.4.2012 I tried to convert it to int and then add it up, but there is a problem when it's up to 30.
Can you tell me some clever way how to do it?
You want to leave it as a DateTime until you are ready to convert it to a string.
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
First, always keep the data in it's native type until you are ready to either display it or serialize it (for example, to JSON or to save in a file). You wouldn't convert two int variables to strings before adding or multiplying them, so don't do it with dates either.
Staying in the native type has a few advantages, such as storing the DateTime internally as 8 bytes, which is smaller than most of the string formats. But the biggest advantage is that the .NET Framework gives you a bunch of built in methods for performing date and time calculations, as well as parsing datetime values from a source string. The full list can be found here.
So your answer becomes:
Get the current timestamp from DateTime.Now. Use DateTime.Now.Date if you'd rather use midnight than the current time.
Use AddDays(7) to calculate one week later. Note that this method automatically takes into account rolling over to the next month or year, if applicable. Leap days are also factored in for you.
Convert the result to a string using your desired format
// Current local server time + 7 days
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
// Midnight + 7 days
DateTime.Now.Date.AddDays(7).ToString("dd.MM.yy");
And there are plenty of other methods in the framework to help with:
Internationalization
UTC and timezones (though you might want to check out NodaTime for more advanced applications)
Operator overloading for some basic math calcs
The TimeSpan class for working with time intervals
Any reason you can't use the AddDays method as in
DateTime.Now.AddDays(7)

function which divides time period by periodicity

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.

Categories