Calculate Days to Expire in Field

Hi,

I am trying to calculate Days to expire based on fields end_date and todays date.

I want the counter to display days left to expire.

I am using the following code

frappe.ui.form.on('bid_proposals', {
    refresh: function(frm) {
        calculate_days_expiring_in(frm);
    },
    end_date: function(frm) {
        calculate_days_expiring_in(frm);
    }
});

function calculate_days_expiring_in(frm) {
    if (frm.doc.end_date) {
        let endDate = new Date(frm.doc.end_date);
        endDate.setHours(0, 0, 0, 0);
        let today = new Date();
        today.setHours(0, 0, 0, 0);
        let diffTime = endDate - today;
        let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
        frm.set_value('expires_in', diffDays);
    }
}

Help is appreciated.

Thanks

You can use frappe utility.

function calculate_days_expiring_in(frm) {
    if (frm.doc.end_date) {
        let diffDays= frappe.datetime.get_diff(frm.doc.end_date,frappe.datetime.now_date())
        frm.set_value('expires_in', diffDays);
    }
}
1 Like

Thanks for your response. I tried the code in client script but when I tested it the output remains 0.

The Field I have created is a Int type field. would that be causing it?

Your help is greatly appreciated.

frappe.ui.form.on('bid_proposals', {
    refresh: function(frm) {
        calculate_days_expiring_in(frm);
    },
    end_date: function(frm) {
        calculate_days_expiring_in(frm);
    }
});
	    
	function calculate_days_expiring_in(frm) {
    if (frm.doc.end_date) {
        let diffDays= frappe.datetime.get_diff(frm.doc.end_date,frappe.datetime.now_date())
        frm.set_value('expires_in', diffDays);
    }
	}

No, that should not be a problem.

See :

run this code in your browser console.

 let diffDays= frappe.datetime.get_diff(cur_frm.doc.end_date,frappe.datetime.now_date())

And also one suggestion is you can make this expires_in field virtual and add depends on.

Example:

{
 "actions": [],
 "allow_rename": 1,
 "creation": "2024-05-29 22:01:47.026196",
 "doctype": "DocType",
 "engine": "InnoDB",
 "field_order": [
  "details_section",
  "expire_date",
  "expires_in"
 ],
 "fields": [
  {
   "fieldname": "details_section",
   "fieldtype": "Section Break",
   "label": "Details"
  },
  {
   "fieldname": "expire_date",
   "fieldtype": "Date",
   "label": "Expire Date"
  },
  {
   "depends_on": "eval:doc.expire_date",
   "fieldname": "expires_in",
   "fieldtype": "Int",
   "label": "Expires In"
  }
 ],
 "index_web_pages_for_search": 1,
 "links": [],
 "modified": "2024-05-29 22:11:54.491838",
 "modified_by": "Administrator",
 "module": "Forum Issue",
 "name": "Expires",
 "owner": "Administrator",
 "permissions": [
  {
   "create": 1,
   "delete": 1,
   "email": 1,
   "export": 1,
   "print": 1,
   "read": 1,
   "report": 1,
   "role": "System Manager",
   "share": 1,
   "write": 1
  }
 ],
 "sort_field": "creation",
 "sort_order": "DESC",
 "states": []
}

Reference: Virtual DocField

2 Likes

Thanks, it worked.