Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Programmatically assign a date object in a date field

Séb P.
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.
September 5, 2013

Hi,

I have a plugin that extend the Create Issue operation. I need to programmatically, in javascript, set a date field to a value based on complex rules.

But when I try to use eg. AJS.$('#customfieldxxx').val(dateObject), it obviously doesn't work since it has to respect the pattern (dd/MMM/yy).

How can I set a date object to a date field?

Thanks in advance!

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
wurbanski
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.
September 5, 2013

First, add jira.webresources:jquery-livestamp as a dependency in your plugin.

Then you should have available moment.js library and JIRA.translateSimpleDateFormat function. The latter will translate JIRA's date/time format into what moment accepts. Then you can use it like this:

var m = moment(new Date());
var dateString = m.format(JIRA.translateSimpleDateFormat(AJS.Meta.get("date-dmy")));
$('#datePicker').val(dateString);

Check out moment.js' website for info on how to use it.
AJS.Meta takes value of the meta tag in the document with name attribute equal to ajs-date-dmy. Check out the page source for more such attributes (including different variants of the date format).

Séb P.
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.
September 5, 2013

This works like a charm :) Thank you very much!

Jobin Kuruvilla [Adaptavist]
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.
May 9, 2014

Awesome solution. Liked it so much that I ended up posting about this here. At least for my own benefit ;)

Jeremy Jao June 27, 2016

Is there official documentation we can read for the JIRA and AJS global variable?

I found partial documentation of the AJS global variable but I don't see everything about it here: https://docs.atlassian.com/aui/latest/docs/

Carsten Hilber
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 6, 2016

I am wondering why you use the 'date-dmy' configuration, JIRA has a configured property for the date input fields called jira.date.picker.java.format. Is there any way to get this property from/through AJS?

1 vote
RambanamP
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.
September 5, 2013

something like you have to try with javascript

var now = new Date();
    var day = ("0" + now.getDate()).slice(-2);
    var month = ("0" + (now.getMonth() + 1)).slice(-2);
    var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
   $('#datePicker').val(today);

check this

http://stackoverflow.com/questions/12346381/set-date-in-input-type-date

http://stackoverflow.com/questions/9921944/set-initial-value-in-datepicker-with-jquery

0 votes
Séb P.
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.
September 5, 2013

Yeah but I would like to comply to the date format globally defined in Jira (not constructing the string myself, but using an api that hide the complexity from my side by just passing the date object).

That should be available somewhere, but I don't know where :)

TAGS
AUG Leaders

Atlassian Community Events