CFLib.org – Common Function Library Project

offsetDate(offset[, refDate])

Last updated February 07, 2013

author

Loïc Mahieu

Version: 1 | Requires: CF9 | Library: DateLib

Description:
Offsets a date according to a passed-in mask. The mask is of format: [value][datepart] eg: 1d (one day) The value must be an integer, and the datePart must be one of "y" for years, "m" for months, "w" for weeks, "d" for days, "h" for hours, "n" for minutes and "s" for seconds. Multiple mask items can be specified, eg: 1d2h3s is one day, two hours and three seconds.

Return Values:
Returns a date offset as per the mask

Example:

// SAMPLES

offsetDate("1d"); // == dateAdd("d", 1, now())
offsetDate("1y", "2013-01-01") // == "2014-01-01"
offsetDate("1m1d") // == Add 1 month and 1 day to now()
offsetDate("-43Y0M+10D") // == subtracts 43 years, adds zero months and ten days to now()


// TESTS

function assert(val1, val2) { if( !dateCompare(val1, val2) == 0 ) throw('"#val1#" != "#val2#"'); }

assert(offsetDate("1y", "2013-01-01"), "2014-01-01");
assert(offsetDate("1m", "2013-01-01"), "2013-02-01");
assert(offsetDate("1w", "2013-01-01"), "2013-01-08");
assert(offsetDate("1d", "2013-01-01"), "2013-01-02");
assert(offsetDate("1h", "2013-01-01 00:00:00"), "2013-01-01 01:00:00");
assert(offsetDate("1n", "2013-01-01 00:00:00"), "2013-01-01 00:01:00");
assert(offsetDate("1s", "2013-01-01 00:00:00"), "2013-01-01 00:00:01");
assert(offsetDate("1y1d1n", "2013-01-01 00:00:00"), "2014-01-02 00:01:00");

Parameters:

Name Description Required
offset A string containing a date mask (as per description) Yes
refDate A date. Defaults to now() No

Full UDF Source:

/**
 * Offset a date according to a datepart defined by a mask.
 * v0.9 by Loïc Mahieu
 * v1.0 by Adam Cameron (improved regex, removed redundant code, renamed function to be less vague)
 * 
 * @param offset      A string containing a date mask (as per description) (Required)
 * @param refDate      A date. Defaults to now() (Optional)
 * @return Returns a date offset as per the mask 
 * @author Loïc Mahieu (loic@igloo.be) 
 * @version 1.0, February 7, 2013 
 */
function offsetDate(required string offset, date refDate = now()) {
    var matcher = createObject("java", "java.util.regex.Pattern")
        .compile("(?i)([+-]?[0-9]+)\s*([snhdwmy])")
        .matcher(offset);

    while (matcher.find()) {
        var match = matcher.group(1);
        var datePart = matcher.group(2);

        if (datePart == "y"){
            datePart = "yyyy";
        }
        else if (datePart == "w"){
            datePart = "ww";
        }
        refDate = dateAdd(datePart, match, refDate);
    }
    return refDate;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson