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)

Getting the month from a date/time field with javascript

(0) ShareShare
ReportReport
Posted on by 60

How do I get the month from a date/time field with javascript? What I need is to get the month from the field and put it as the record name, thank you.

*This post is locked for comments

I have the same question (0)
  • alf Profile Picture
    17,915 on at

    var d = new Date();

    var curr_date = d.getDate();

    var curr_month = d.getMonth();

    var curr_year = d.getFullYear();

  • BeWood Profile Picture
    60 on at

    I need the date from the field, not today's current date, eg: field says 02/31/2010, the name of the record will have February.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello.

    Try to use following script:

    if (crmForm.all.<date field>.DataValue == null)

    return;

    var monthnames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

    var name = monthnames[crmForm.all.<date field>.DataValue.getMonth()];

  • Cyclefitness Profile Picture
    on at

    I need a simplified version of this script that gets the month, in number format, from a specific date field named in the function.

  • Suggested answer
    Neil Parkhurst Profile Picture
    10,727 User Group Leader on at

    If you want this as a function try something like this;

     // ** This code takes createdon date, passes to a function.

     var anyDate = Xrm.Page.getAttribute("createdon").getValue();

     var returnedMonth = ReturnMonth(anyDate);

      alert(returnedMonth);

    // ** A simple function to return month number  

    function ReturnMonth(aDate) {

      aDate = new Date(aDate);

      // 1 is added as months start at 0!

      return aDate.getMonth() + 1;

    }

  • Cyclefitness Profile Picture
    on at

    Thank you, Neil!!!

  • Inogic Profile Picture
    703 on at

    function getDateMonth()

    {

       //get date

       var date = Xrm.Page.getAttribute("new_date").getValue();      

       var d = new Date(date);

       //show Month in alert window

       Xrm.Utility.alertDialog("Month is : "+ (d.getMonth()+1));

    }

    Note : The index position of getMonth() method starts from 0. So to complete all the 12 months we need to add 1 into it. Otherwise for December month the output would have been 11 instead of 12.

    Thanks,

    Sam

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi BeWood,

    var date = Xrm.Page.getAttribute(yourDateField).getValue();

    var objDate = new Date(date),

       locale = "en-us",

       month = objDate.toLocaleString(locale, { month: "long" });

    Xrm.Page.getAttribute(YourRecordFieldName).setValue(month);

    //alert(month);

    Thank you,

    If my solution is answered you Question mark it as "Answered".

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