Input date with a button click

This sample inserts today's date into a Date field of your App with a click of a button.

To use this sample code, you will need a Date field and a Blank Space field in your kintone App.
A button will be displayed on the Blank Space field, when you create a new record or edit an existing record. 

Sample Image

inputdate.gif 

Sample Code

// A button will be placed on the Blank Space field
// Click the button to add today's date into the date field

(function() {
    "use strict";
    var DATE = 'mydate'; // field code of date field
    var SPACE = 'myspace'; // element ID of Blank Space field (this is where the button will be displayed)

    kintone.events.on(['app.record.create.show', 'app.record.edit.show'], function(event) {
        var btn = document.createElement('button');
        btn.textContent = "Input Today's Date";
        kintone.app.record.getSpaceElement(SPACE).appendChild(btn);

        btn.onclick = function() {
            var date = new Date();
            var today = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();

            var rec = kintone.app.record.get();
            rec.record[DATE].value = today;
            kintone.app.record.set(rec);
        };
        return event;
    });
})();

Notes

Make sure to match the Field code / Element ID of your fields, with the Field code / Element ID written in the code.

 

Refer to the Tutorial page, on how to add scripts to your app.

Was this article helpful?
0 out of 0 found this helpful
Comments
Please sign in to leave a comment.