Date Calculation JS issue

I keep getting the error message “moment is undefined” in the following JS.

(function() {

    var FieldID1 = 'ID'
    var FieldID2 = 'DateA2'
    var FieldID3 = 'DateA3'
    var FieldID4 = 'DateNY'
	var events = ["app.record.create.show","app.record.edit.change." + FieldID1,"app.record.create.change." + FieldID1];
	kintone.events.on(events, function(event) {
        
        var record = event.record;
        var startdate = record[FieldID1].value
        
        if (startdate != 0){ if (startdate != NaN){ if (startdate != null){
        var A2 = moment(startdate, "YYYY-MM-DD").add(4,'months').format("YYYY-MM-DD");
        var A3 = moment(A2, "YYYY-MM-DD").add(4,'months').format("YYYY-MM-DD");
        var NY = moment(A3, "YYYY-MM-DD").add(4,'months').format("YYYY-MM-DD");
        console.log(startdate)
        console.log(A2)
        console.log(A3)
        console.log(NY)

         record[FieldID2].value = A2
         record[FieldID3].value = A3
         record[FieldID4].value = NY
         return event;
         }}}
    });	
}());

So I figured out the initial problem. I needed to install moment.js with my JS. I did that then tried my code on a different app with different field codes and a different length of time.  It will not work on the new app. It won’t even give me an error message. Without an error message I have no idea how to troubleshoot.

(function() {

    var FieldID1 = 'ES'

    var FieldID2 = 'ED'

    //var FieldID3 = 'DateA3'

    //var FieldID4 = 'DateNY'

    var events = ["app.record.create.show","app.record.edit.change." + FieldID1,"app.record.create.change." + FieldID1];

    kintone.events.on(events, function(event) {

       

        var record = event.record;

        var startdate = record[FieldID1].value

       

        if (startdate != 0){ if (startdate != NaN){ if (startdate != null){

        var A2 = moment(startdate, "YYYY-MM-DD").add(180,'days').format("YYYY-MM-DD");

        //var A3 = moment(A2, "YYYY-MM-DD").add(4,'months').format("YYYY-MM-DD");

        //var NY = moment(A3, "YYYY-MM-DD").add(4,'months').format("YYYY-MM-DD");

        console.log(startdate)

        console.log(A2)

        //console.log(A3)

        //console.log(NY)

         record[FieldID2].value = A2

         //record[FieldID3].value = A3

         //record[FieldID4].value = NY

         return event;

         }}}

    });

}());

Helo Caneel,

The moment.js in Kintone seems to be in maintenance mode, so that might be why.

As mentioned on the following page, it is advised to use a different JavaScript date and time library instead, such as Luxon:

Customize Date Formats - Kintone Developer Program

Luxon

https://moment.github.io/luxon/#/

1 Like

Thank you. That was very helpful.

No problem! Glad we could help you in these trying times.

1 Like