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)

Get Year and Month

(0) ShareShare
ReportReport
Posted on by

Hi,

I am trying to get the Year and Month from a date field using the following script but its not working

Function getdates () {

var startDate = Xrm.Page.getAttribute("dac_typeofcaller").getValue();

if (startDate != null) {

var year = startDate.getFullYear() + "";

var month = (startDate.getMonth()+1)+"";

alert(month);
alert(year);
}

}

The above script results in the following error :

ReferenceError: getdates is not defined
at eval (eval at RunHandlerInternal (https://xxx.crm11.dynamics.com/form/ClientApiWrapper.aspx?ver=-253954455:153:1), <anonymous>:1:1)

The date field is on the form and its populated

Your help is greatly appreciated.

Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Try with this -

           function getdates()
    {
    var crmDate = Xrm.Page.getAttribute("dac_typeofcaller").getValue(); var startDate = new Date(crmDate); if (startDate != null) { var year = startDate.getFullYear() + ""; var month = (startDate.getMonth()+1)+""; alert(month); alert(year); }
    }
  • TonyN Profile Picture
    695 on at

    You just need to change "Function" to "function"

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    The error you are getting is because you have a syntax error in your script. Change "Function" to "function" it should work.

    ==============

    function getdates() {

       var startDate = Xrm.Page.getAttribute("dac_typeofcaller").getValue();

       if (startDate != null) {

           var year = startDate.getFullYear() + "";

           var month = (startDate.getMonth() + 1) + "";

           alert(month);

           alert(year);

       }

    }

    ==========

    You can refer the below article for more details on javascript.

    neilparkhurst.com/.../javascript-basics

    neilparkhurst.com/.../javascript-my-collection

    Hope it helps.

  • Jay2014 Profile Picture
    on at

    year.PNG

    Thank you all but it seems to be defaulting and giving these alerts :

    Month - 1

    Year - 1970

    year.PNG

  • Jay2014 Profile Picture
    on at

    sorted, i was using the wrong field.

  • Suggested answer
    DKasp Profile Picture
    229 on at

    The error is telling you that it can't find getdates - you're doing something wrong with how you're calling that function.

  • gdas Profile Picture
    50,091 Moderator on at

    Please close the thread if your issue got resolved.

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