This week we’re going to look at a couple specific scripts dealing with how to determine dates in SmartConnect. The first one will show how to get the last day of the previous month, and the second will show how to get the last day of the current month. The second script involves an extra step, but using these as a basis you should be able to get any other day you need as well.
Last day of the previous month:
Dim myDate As DateTime = Date.Now.ToString(“MM/01/yyyy”)
myDate = myDate.AddDays(-1)
return myDate
The first thing we do in this script is declare the variable to hold the date, along with assigning it a value. Date.Now gets the current date, but we throw a format onto it in order to modify the date its generating. MM/01/yyyy in the format means we grab the current month and year, but the day is hardcoded to the value 01.
So if we ran the first line of code anytime during this month it would generate 04/01/2013. The second line then is simply setting that date equal to itself minus 1 day, which ends up being the last day of the previous month. By calculating it in this manner we don’t have to worry about whether the previous day had 30 or 31 days (or 28), we let the .Net framework take care of that for us.
Last day of the current month:
Dim myDate As DateTime = Date.Now.ToString(“MM/01/yyyy”)
myDate = myDate.AddMonths(1)
myDate = myDate.AddDays(-1)
return myDate
This second script adds one extra step to the process. We are able to add and subtract months the same way we can with days on any date we have. So before we subtract a day, we add one month to the date so that it will end up being the last day of the current month.
Hopefully these scripts help out when creating new integrations where we need to calculate a date separate from what is in the source file. Using these, you could easily modify them to get the first day of the month or any other day you needed.
Chris Hanson
Senior Technical Consultant