Use Javascript to compare date?

Kelsey Whiting June 17, 2015

I have a custom field that is called "estimated issue close date". If the estimated issue close date has past, I want the field to clear or show a message or do something when the user goes to edit the issue so they must change the date. I can't add a validator because I want it to happen on Edit and there is no workflow transition for that. How can I accomplish it with javascript? Also, I do not have permission to make custom plugins

1 answer

0 votes
Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 18, 2015

Hi Kelsey,

You would have to include some javascript in the announcement banner.  Here is an example that might get you started.  It doesn't filter by project, so this would be active for all projects.  Also, this one uses the due date field.  If you want to use it for your particular due date field, you would have to change "#duedate" to "#customfield_XXXXX" (you would have to look using "Inspect Element" and find the id).  This also assumes your date format is 1/Jan/15. The results of this script are that if the due date is prior to the current date, then an alert is shown and the field is cleared.

jQuery(document).ready(function() {
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e,context) {
        var dueDate = jQuery("#duedate").val();
        dueDate = Date.parse(dueDate.substring(2, 5) + " " + dueDate.substring(0, dueDate.indexOf("/")) + ", 20" + dueDate.substring(6, 8));
        var todaysDate = new Date();
        todaysDate.setHours(0,0,0,0);
        if (dueDate <= todaysDate) {
          jQuery("#duedate").val("");
          alert("The previously selected due date has passed. Please enter a new due date.");
        }
    });
});

I hope you can figure out how to modify to fit your needs. Don't forget to wrap it in <script> tags.

Suggest an answer

Log in or Sign up to answer