Get month on print format

Hello,

Can someone give a tip, i am trying to make a custom print, but on place of date, day-month-year, i only want to print month?

any help?

1 Like

Hi Dany,

I’m not sure how to format this directly in Jinja, but I’ll offer a possible workaround.

Create a new custom field (type: data) in the document. This can be hidden if you only want it on the print format.
Add the following to the custom script for that document, changing the doctype, trigger, and custom field name:

frappe.ui.form.on("DocType", {
    trigger: function(frm) {
        var today = new Date();
        var month = new Array();
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";
        var n = month[today.getMonth()];
        frm.set_value("custom_data_field_for_month", date);
    }
});

Then just add the custom field to your print format.

1 Like

@cpurbaugh

hello

i created a new field called “month” and the date field it suppose to fetch the month from is “posting date” which is in a doctype called “salary slip”

still couldn’t get this right … kindly assist

Try
{{ frappe.utils.formatdate(doc.get_formatted(‘transaction_date’), “MMM”) }}

2 Likes

@mohitchechani

can you throw more light on how to go about this like; where do i try this ??

I’m 5 years late but I was trying to solve the same problem.
Here is what I came up with. Its a bit hacky but gets the job done.

In the print template, you can use this.

{% set months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"] %}
{% set month = (doc.posting_date[5] + doc.posting_date[6])|int - 1 %}

<!-- Then you can show the month like -->
{{ months[month] }}
1 Like

Modified the above to this
{{ frappe.utils.formatdate(doc.get_formatted(‘posting_date’), “MMMM yyyy”) }}

Hi @mohitchechani ,

I’m working on the salary slip and for that I want to print the month name in the salary slip. So, can you please tell me how can I use this script in salary slip to get month and year name based on salary start date

you can use this

{{ frappe.utils.get_datetime(doc.start_date).strftime("%B %Y") }}

Where I need to put this script ? I’m using it inside a client script where I’m trying to set the month and year value inside a custom field. Please see the following code.

frappe.ui.form.on('Salary Slip', {
    refresh(frm) {
	frm.set_value('custom_month', frappe.utils.get_datetime(doc.start_date).strftime("%B %Y"))
    }
})

use this

{{ frappe.utils.formatdate(doc.start_date, “MMM , yyyy”) }}