Custom Time Token to define a School Calendar

I want to start off by I know how to make basic Token adjustments like a ‘Last 90 Days’ which you’d just say it to the day level minus 90 and call it a day but now we have a client that wants a School Cycle date token.

Goal: Create a time token for a date range with an end date or the current date to a start date of the most recent July 1.

  • Example 1: If we selected this custom token today (2019-09-05) we’d expect a range of:
    ** Start Date = July 1, 2019
    ** End Date = Sept. 5, 2019
  • Example 2: If we selected this custom token on June 31, 2019 we’d expect a range of:
    ** Start Date = July 1, 2018
    ** End Date = June 3, 2019

Anyone have a custom time token script that would work for this, even if it needs some tweaking. Unfortunately I’ve had limited exposure/experience in C# so any assistance will be greatly appreciated!

In that case, a script token should return an array of 2 C# DateTime objects - the first element to be the start date and the second element to be the end date. You can reference a specific day when instantiating the object like so:

new DateTime(year, month, day);

and you can use DateTime.Today to get today’s date.

So something like:

DateTime[] dates = new DateTime[2];
dates[0] = new DateTime(2019, 7, 1);
dates[1] = DateTime.Today;
return dates;

Documentation on the DateTime object: https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8

Of course, this will hardcode the year. For your specific use case, you may want to have something like this:

DateTime[] dates = new DateTime[2];
DateTime t = DateTime.Today; // Get today's date
int y = t.Year; // Get current year
if(DateTime.Today.Month < 7) { 
   year--; // if today's date falls before July, it cycles to 7/1 of previous year
}
dates[0] = new DateTime(year, 7, 1);
dates[1] = DateTime.Today;
return dates;
2 Likes

Thanks, that’s marvelous. With a little tweaking only cause year wasn’t declared but yeah this is what I ended up using thanks to your help! Greatly appreciated!!!

DateTime[] dates = new DateTime[2];
DateTime t = DateTime.Today; // Get today’s date
int y = t.Year; // Get current year
if(DateTime.Today.Month < 7) {
y–; // if today’s date falls before July, it cycles to 7/1 of previous year
}
dates[0] = new DateTime(y, 7, 1);
dates[1] = DateTime.Today;
return dates;

1 Like