Skip to content
SamuelPlentz edited this page Feb 7, 2020 · 1 revision

Gets the current date with an offset.

Script

// default values for variables
if(mVariables.length < 1) mVariables[0] = "0"; // number of months to add or subtract
if(mVariables.length < 2) mVariables[1] = "0"; // number of days to add or subtract
if(mVariables.length < 3) mVariables[2] = "short"; // format to use


// get the current date with an offset (specified by the variables)
var date = new Date();
var dayOffset = date.getDate() - 1;
var dateOffset = new Date(date.getFullYear(), date.getMonth(), 1);
dateOffset.setMonth(dateOffset.getMonth() + parseInt(mVariables[0]));
dateOffset.setDate(dateOffset.getDate() + dayOffset + parseInt(mVariables[1]));


// options described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
let                                            options = {                  year: "numeric", month: "long",    day: "numeric" };
if(mVariables[2].toLowerCase() == "numeric")   options = {                  year: "numeric", month: "2-digit", day: "2-digit" };
if(mVariables[2].toLowerCase() == "short")     options = {                  year: "numeric", month: "long",    day: "numeric" };
if(mVariables[2].toLowerCase() == "long")      options = { weekday: "long", year: "numeric", month: "long",    day: "2-digit" };
if(mVariables[2].toLowerCase() == "yearmonth") options = {                  year: "numeric", month: "long"                    };
if(mVariables[2].toLowerCase() == "year")      options = {                  year: "numeric"                                   };
if(mVariables[2].toLowerCase() == "monthname") options = {                                   month: "long"                    };
if(mVariables[2].toLowerCase() == "weekday")   options = { weekday: "long"                                                    };


// return result; undefined instead of "en-EN" or "de-DE" should use the users language by default
return dateOffset.toLocaleDateString(undefined, options);

Usage

[[SCRIPT=DateOffset|monthOffset|dayOffset|type]]

monthOffset optional The number of months to add or subtract. (default is 0)
dayOffset optional The number of days to add or subtract. (default is 0)
type optional The type may be numeric, short (default), long, yearmonth, year, monthname or weekday. The actually inserted value depends on the language of your OS.

Examples:

Assuming it is "09/18/2020" today.

[[SCRIPT=DateOffset|5]] would add 5 month and would be resolved to: February 18, 2021

[[SCRIPT=DateOffset|0|14|long]] would add 14 days and would be resolved to: Friday, October 02, 2020

[[SCRIPT=DateOffset|0|-3|numeric]] would subtract 3 days and would be resolved to: 09/15/2020

[[SCRIPT=DateOffset|12]] would add 1 year and would be resolved to: September 18, 2021

Border Cases when adding months:

Assuming it is "02/29/2020" today.

[[SCRIPT=DateOffset|12]] would add 1 year and would be resolved to: March 1, 2021

Assuming it is "01/31/2020" today.

[[SCRIPT=DateOffset|1]] would add 1 month and would be resolved to: March 2, 2020

[[SCRIPT=DateOffset|3]] would add 3 month and would be resolved to: May 1, 2020