How to convert Duration into Date

Hi, is there any way to convert the Duration Slot, which returns something like PT15M, to a time, in this example, 15 minutes from now?

Thanks

you can use code block. This might not be perfect but you can try and fix.

var iso8601DurationRegex = /^(-)?P(?!$)(\d+(?:\.\d+)?Y)?(\d+(?:\.\d+)?M)?(\d+(?:\.\d+)?W)?(\d+(?:\.\d+)?D)?(T(?=\d)(\d+(?:\.\d+)?H)?(\d+(?:\.\d+)?M)?(\d+(?:\.\d+)?S)?)?$/;

function parseISO8601Duration (iso8601Duration) {
    var matches = iso8601Duration.match(iso8601DurationRegex);
    //console.log(matches);
    return {
        sign:    matches[1] === undefined ? '+' : '-', // not used
        years:   matches[2] === undefined ? 0 : parseInt(matches[2]),
        months:  matches[3] === undefined ? 0 : parseInt(matches[3]),
        weeks:   matches[4] === undefined ? 0 : parseInt(matches[4]),
        days:    matches[5] === undefined ? 0 : parseInt(matches[5]),
        hours:   matches[7] === undefined ? 0 : parseInt(matches[7]),
        minutes: matches[8] === undefined ? 0 : parseInt(matches[8]),
        seconds: matches[9] === undefined ? 0 : parseInt(matches[9])
    };
};

// slot value captured should be put into {duration}
//var duration = 'PT10M'
var dr = parseISO8601Duration(duration);

var now = new Date();
var dt = new Date(now.getTime());

if (dr.years) {
    dt.setFullYear(dt.getFullYear() + dr.years);
}
if (dr.months) {
    dt.setMonth(dt.getMonth() + dr.months);
}
if (dr.weeks || dr.days) {
  dt.setDate(dt.getDate() + dr.weeks * 7 + dr.days);
}
if (dr.hours){
  dt.setHours(dt.getHours() + dr.hours);
}
if (dr.minutes) {
  dt.setMinutes(dt.getMinutes() + dr.minutes);
}
if (dr.seconds) {
  dt.setSeconds(dt.getSeconds() + dr.seconds);
}

// for debug
//console.log(`NOW       : ${now}`);
//console.log(`CALCULATED: ${dt}`);

// now you can manipulate "dt" as JavaScript Date object. Retrieve each object in "dt", combine, and put those into Voiceflow variables.
// ...
1 Like