datePicker in scriptcode

How to get and set value of datePicker in scriptcode?

datePicker’s value is timestamp. You can easily convert timestamp to date.
Example:

    //getting value of component
    var timestamp = context.getFirstParameter("datePicker"); //1669633200000
    //converting options
    var date = new Date(timestamp) //Mon Nov 28 2022 12:00:00 GMT+0100 (CET)
    var formattedDate = date.toLocaleDateString("pl-PL") //November 28, 2022
    var hours = date.getHours(); //12
    var minutes = "0" + date.getMinutes(); //00
    var seconds = "0" + date.getSeconds(); //00

To pass value from script to datePicker You have to do opposite conversion.
Example:

    //Date to select
    var myDate = "28-11-2022";
    //create new Date object
    myDate = myDate.split("-");
    var newDate = new Date( myDate[2], myDate[1] - 1, myDate[0]);
    //convert date to timestamp
    var timestamp  = newDate.getTime() //1669590000000
    return [{'output': timestamp}];
2 Likes