How to get the string value of month and year in a date

HI,

Can somebody help me how to get the string value of month and year in a date which I can use it in my client script and it should output like this “JUN-2023” if the date is 06-1-2023. I want to replace my field “period_covered” (data type) with something like that. Thank you.

Regards,

Edmund

Hi @EdmundDelima,

Please apply the client script for it.

frappe.ui.form.on('Your DocType', {
    refresh: function(frm) {
        // Get the value of the your_field_name field
        var dt = frm.doc.your_field_name;
        // eg. var dt = 01-06-2023 (DD/MM/YYYY)

        // Convert the date to the desired format
        var formattedDate = frappe.datetime.str_to_user(dt);
        var dateParts = formattedDate.split('-');
        var Month = getMonth(dateParts[1]);

        // Format the date with the month
        var FormattedDate = Month + '-' + dateParts[2];

        // Set the formatted date in a custom field
        frm.set_value('custom_date_field', FormattedDate);
        console.log("------------", FormattedDate);
        // Output: JUN-2023
    }
});

// Function to get the month name
function getMonth(month) {
    var Months = {
        '01': 'JAN',
        '02': 'FEB',
        '03': 'MAR',
        '04': 'APR',
        '05': 'MAY',
        '06': 'JUN',
        '07': 'JULY',
        '08': 'AUG',
        '09': 'SEP',
        '10': 'OCT',
        '11': 'NOV',
        '12': 'DEC'
    };

    return Months[month];
}

Please set your doctype name and field name according.

Reference:

Thank You!

Hi @NCP thank you so much for this. Case solved.