The CreatorCon Call for Content is officially open! Get started here.

Unable to use .getByFormat("MM-dd-yyyy"); in script include

Kumar38
Kilo Sage

I am passing 3 date fields to script include from a client script . Though the dates are passed in the same format ("MM-dd-yyyy") as I require.

I'd like to ensure , it doesn't affect users from various places . hence using .getByFormat("MM-dd-yyyy") . But it doesnt work as I printed logs and it prints undefined

var ahm_eos = this.getParameter('sysparm_ahm_eos');
        var ahm_eol = this.getParameter('sysparm_ahm_eol');
        var ahm_tspd = this.getParameter('sysparm_ahm_tspd')


ahm_eos = ahm_eos.getByFormat("MM-dd-yyyy");
        ahm_eol = ahm_eol.getByFormat("MM-dd-yyyy");
        ahm_tspd = ahm_tspd.getByFormat("MM-dd-yyyy");

// THE ABOVE VALUES ARE PRINTED AS UNDEFINED IN LOGS
7 REPLIES 7

Year is being passed with only the last 2 digits.

Seems like sysparm_ahm_eos = '22-03-29' and not "2022-03-29'.

The date format of the end user may be "MM-dd-yy"?

Try logging the parameters out to confirm.

var ahm_eos = this.getParameter('sysparm_ahm_eos');
var ahm_eol = this.getParameter('sysparm_ahm_eol');
var ahm_tspd = this.getParameter('sysparm_ahm_tspd');

gs.info('ahm_eos:' + ahm_eos);
gs.info('ahm_eol:' + ahm_eol);
gs.info('ahm_tspd:' + ahm_tspd);

var gdAhmEos = new GlideDate();
var gdAhmEol = new GlideDate();
var gdAhmTspd = new GlideDate();
gdAhmEos.setValue(ahm_eos);
gdAhmEol.setValue(ahm_eol);
gdAhmTspd.setValue(ahm_tspd);

ahm_eos = gdAhmEos.getByFormat("MM-dd-yyyy");
ahm_eol = gdAhmEol.getByFormat("MM-dd-yyyy");
ahm_tspd = gdAhmTspd.getByFormat("MM-dd-yyyy");

Paste the Client Script calling this Script Include so I'll be able to see how the date parameters are being set. I think the remaining problem is with the Client Script.

Not too sure but try replacing .setValue() with .setDisplayValue(). The parameters, now, should be set in end-user's date format.

var ahm_eos = this.getParameter('sysparm_ahm_eos');
var ahm_eol = this.getParameter('sysparm_ahm_eol');
var ahm_tspd = this.getParameter('sysparm_ahm_tspd');

var gdAhmEos = new GlideDate();
var gdAhmEol = new GlideDate();
var gdAhmTspd = new GlideDate();
gdAhmEos.setDisplayValue(ahm_eos);
gdAhmEol.setDisplayValue(ahm_eol);
gdAhmTspd.setDisplayValue(ahm_tspd);

ahm_eos = gdAhmEos.getByFormat("MM-dd-yyyy");
ahm_eol = gdAhmEol.getByFormat("MM-dd-yyyy");
ahm_tspd = gdAhmTspd.getByFormat("MM-dd-yyyy");