web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

momentjs calculate the age between two date fields

(0) ShareShare
ReportReport
Posted on by

I am attempting to calculate the age between two date fields - so for example if the created on date is 6/15/10 and the sold date (manual entry) is 6/20/10, I'd like to get the age or difference between the two dates.

Now with that said I've been directed to look at momentjs and I have done my homework, however I am new developer and I'm really struggling with this one guys so if someone could help I would greatly appreciate it. I do NOT ask for code nor do I come here without doing all my research before asking questions, and I understand momentjs is what I need, however I'm not sure how to format it is all, please help, thanks much.

*This post is locked for comments

I have the same question (0)
  • Aileen Gusni Profile Picture
    44,524 on at

    Adam,

    You need javascript in CRM form scripting?

    Or you want a batch job?

    Thanks.

  • Community Member Profile Picture
    on at

    Aileen yea the first one, JS in CRM form Scripting. Thanks very much.

  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at

    Adam,

    Walkthrough the following article. It explains deeply about moment.js

    www.c-sharpcorner.com/.../moment-js

    Hope this helps.

    Regards,

    R.Rajkumar

    "Please mark my answer as verified if you found it helpful"

  • Aileen Gusni Profile Picture
    44,524 on at

    Adam,

    I am sure and believe that you have searched internet before.

    But, maybe you can refer to these articles

    arunpotti.wordpress.com/.../calculate-difference-between-two-dates-using-javascript

    crmbusiness.wordpress.com/.../crm-2011-javascript-date-difference-code

    Hope this can help you, if not just please ask again.

    Thanks.

  • Community Member Profile Picture
    on at

    hey guys thanks a lot, do I need to import the moment.js library into CRM somehow before I use it or can I just start coding?

  • Community Member Profile Picture
    on at

    I have attempted both options  (momentjs and some code from a link provided by Aileen),

    momentjs code

    function calculateDays() {

    var createdon = Xrm.Page.getAttribute("createdon").getValue(); // get the createdOn Date value
    var solddate = Xrm.Page.getAttribute ("new_soldtobankdate").getValue(); // get the Sold Date value
    var salesstage = Xrm.Page.getAttribute("new_salesstagercd").getValue(); // get Sales Stage value

    alert(createdon);
    alert(solddate);
    alert(salesstage);

    var m_date = moment(createdon); // create the Moment.js object

     if (salesstage == 100000008) {
     var days = moment().diff('1981-01-01', 'days');
     
     alert(days)
     
     // Xrm.Page.getAttribute("new_pipelineage").setValue(days);
     
       }
     }

    The result of this is,

    createdon date:

    error3.GIF

    sold date:

    sold-date.GIF

    sales stage:

    prompt3.GIF

    error msg:

    error3.GIF

    Other code I tried

    function calculateDays(datetime1, datetime2) {

    var createdOn = Xrm.Page.getAttribute("createdon").getValue(); // get the createdOn Date value

    var solddate = Xrm.Page.getAttribute ("new_soldtobankdate").getValue(); // get the Sold Date value

    var oneDay = 1000 * 60 * 60 * 24;        // The number of milliseconds in a day

    //Convert the datetime1 and datetime2 to Date object and get Time in milliseconds

    var dt1 = new Date(datetime1).getTime();

    var dt2 = new Date(datetime2).getTime();

    // Calculate the difference in milliseconds

    var diff = Math.abs(dt1- dt2); // Difference of Days

    alert(diff);

    return Math.round(diff / oneDay);

    }

    The result of this is, prompt2.GIF

  • Community Member Profile Picture
    on at

    Guido I'm not sure how to include the momentjs library so I would say no - is there a KB or a site you could refer me to or how do I do this? sorry man I'm new at this, thanks in advance.

    I want to get the difference in days.

    thanks for the help.

  • Rajkumar Rajaraman Profile Picture
    on at

    Hi Adam,

    Refer this article. It explains deeply

    www.crmanswers.net/.../momentjs-and-dynamics-crm.html

    Hope this helps.

    Regards,

    R.Rajkumar

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans