J tricks - Little JIRA Tricks
  • Home
  • Plugins ↓
    • JQL Tricks Plugin
      • JQL Tricks Plugin - Cloud
        • JQLT Cloud Installation
        • JQLT Cloud Configuration
        • JQLT Cloud Usage
        • JQLT Cloud License
        • JQLT Cloud FAQ
      • JQL Tricks Plugin - DC
        • JQLT DC Installation
        • JQLT DC Configuration
        • JQLT DC Usage
          • JQLT Issue Functions
          • JQLT Subtask Functions
          • JQLT Links Functions
          • JQLT Development Functions
          • JQLT Worklog Functions
          • JQLT Project Functions
          • JQLT Component Functions
          • JQLT Version Functions
          • JQLT Group Functions
          • JQLT User Functions
          • JQLT Date Functions
        • JQLT DC License
        • JQLT DC FAQ
        • JQLT DC Known Issues
        • JQLT DC Performance
      • JQL Tricks Cloud Migration
    • Simplified Planner
      • J-Planner Installation
      • J-Planner Configuration
      • J-Planner Usage
        • Creating a plan
        • Editing a plan
        • Deleting a plan
        • Viewing a plan
        • Modifying a plan
      • J-Planner FAQ
    • Atla-Search Plugin
      • Atla-Search Installation
      • Atla-Search Configuration
      • Atla-Search Usage
      • Atla-Search License
      • Atla-Search FAQ
    • Copy to subtask Plugin
    • All Plugins
  • Tutorials
  • The Book
  • Contact Us
  • Home
  • Plugins ↓
    • JQL Tricks Plugin
      • JQL Tricks Plugin - Cloud
        • JQLT Cloud Installation
        • JQLT Cloud Configuration
        • JQLT Cloud Usage
        • JQLT Cloud License
        • JQLT Cloud FAQ
      • JQL Tricks Plugin - DC
        • JQLT DC Installation
        • JQLT DC Configuration
        • JQLT DC Usage
          • JQLT Issue Functions
          • JQLT Subtask Functions
          • JQLT Links Functions
          • JQLT Development Functions
          • JQLT Worklog Functions
          • JQLT Project Functions
          • JQLT Component Functions
          • JQLT Version Functions
          • JQLT Group Functions
          • JQLT User Functions
          • JQLT Date Functions
        • JQLT DC License
        • JQLT DC FAQ
        • JQLT DC Known Issues
        • JQLT DC Performance
      • JQL Tricks Cloud Migration
    • Simplified Planner
      • J-Planner Installation
      • J-Planner Configuration
      • J-Planner Usage
        • Creating a plan
        • Editing a plan
        • Deleting a plan
        • Viewing a plan
        • Modifying a plan
      • J-Planner FAQ
    • Atla-Search Plugin
      • Atla-Search Installation
      • Atla-Search Configuration
      • Atla-Search Usage
      • Atla-Search License
      • Atla-Search FAQ
    • Copy to subtask Plugin
    • All Plugins
  • Tutorials
  • The Book
  • Contact Us

Setting formatted dates using javascript in JIRA

5/10/2014

2 Comments

 
Wow, it's been a while since the last post. Crazy schedule but this one is too cool to ignore!

Did you ever want to populate a date field in JIRA but with all the formatting defined in JIRA itself? And maybe add, subtract days etc using javascript?

Well, it is easier than you think. And no need to write crazy javascript code to do all the calculations by yourself. Just offload the work to Moment.js! And that is exactly what JIRA does, thanks to Wojciech Urbanski from Atlassian for pointing that out in this post.

For example, if you want to set the current date to "due date" in an issue, you can use this:

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


That's all!

How about if you want to set the due date 5 days from now? It is again Moment.js that comes to the rescue.

var due = moment().add('days', 5);
var dateString = due.format(JIRA.translateSimpleDateFormat(AJS.Meta.get("date-dmy")));
AJS.$('#duedate').val(dateString);


What about setting the date and time on a datetime custom field instead of just the date? Use date-complete instead of date-dmy.

var due = moment(new Date());
var dateString = due.format(JIRA.translateSimpleDateFormat(AJS.Meta.get("date-complete")));
AJS.$('#duedate').val(dateString);


You can find the other available formats at atlassian-jira/includes/jquery/plugins/livestamp/moment.jira.i18n.js. Or checkout the page source for more such attributes that can be retrieved via AJS.Meta.get().

Yup, that is what I thought. Cool stuff!
2 Comments
Vasiliy link
9/2/2014 11:26:27 pm

Hi,
When I tried AJS.Meta.get("some tag") in custom plugin gadget code it returns empty result. When the same code is out of gadget scope - it works well. Any thoughts on missing dependencies or anything else?

Regards,
Vasiliy

Reply
J-Tricks
9/7/2014 01:32:18 am

What if you add the following?

#requireResource("com.atlassian.jira.gadgets:common").

or

#requireResource("com.atlassian.gadgets.publisher:ajs-gadgets")

I haven't tried it, so not sure.

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    Enter your email address:

    Author

    Jobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. 

    Author of JIRA Development Cookbook and JIRA 5.x Development Cookbook.


    RSS Feed

    Categories

    All
    Acive Objects
    Ajs
    Book
    Components
    Condition
    Custom Fields
    Customization
    Events
    Gadgets
    Javascript
    Jql
    Listener
    Mail
    Permissions
    Plugin Framework
    Post Function
    Properties
    Remote Invocation
    Reporting
    Rest
    Scheduled Tasks
    Search
    Services
    Soap
    Summit
    User Interface
    Validator
    Webwork Actions
    Workflow

    Archives

    October 2016
    August 2016
    March 2016
    January 2016
    December 2015
    May 2014
    December 2013
    November 2013
    July 2013
    June 2013
    April 2013
    October 2012
    September 2012
    August 2012
    July 2012
    May 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011
    June 2011
    May 2011
    April 2011
    March 2011
    February 2011
    January 2011
    November 2010
    October 2010
    September 2010
    August 2010

SUPPORT
APPS
TUTORIALS
THE BOOK
© J-Tricks