Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Auto-populate text fields based on a date field

(0) ShareShare
ReportReport
Posted on by

Hi Community,

On my Opportunity form I have a custom date field .

I also have 2 custom text fields called Month and Year.

How do I auto-populate the Month and Year fields with values based on the date field?

Example 1:

Date Field user selects 01/01/2014

Month auto-populates with 'January'
Year auto populates with '2014'

Example 2:

Date Field user selects 12/12/2015

Month auto-populates with 'December'
Year auto populates with '2015'


Cheers,
VVG

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Auto-populate text fields based on a date field

    Thanks Guido!

  • Royal King Profile Picture
    27,686 on at
    RE: Auto-populate text fields based on a date field
    function FormatDate(fieldname) {
        var d = Xrm.Page.data.entity.attributes.get(fieldname).getValue();
        if (d != null) {
                   var months =  ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
            var  date = d.getDate();
            var curr_month = d.getMonth();       
            var year = d.getFullYear();
            var month= months[curr_month] ;
        }
        else return null;
    }
  • Suggested answer
    Saroj Das Profile Picture
    3,355 on at
    RE: Auto-populate text fields based on a date field

    Hello, on dateField onchange event call the function. If required, modify as per your requirement.

    function getMonthYear(){

    var dateVal=Xrm.Page.getAttribute(dateField).getValue();

    var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];

    if(dateVal!=null ){

    var data=dateVal.split('/');

    if(data.length>0){

    // for example your date is in dd/mm/yyyy format

    // 31/12/2013

    var mth=months[data[1]-1];

    var yr=data[2];

    Xrm.Page.getAttribute('monthField').setValue(mth);

    Xrm.Page.getAttribute('yearField').setValue(yr);

    }

    }

    }

    hth

    Thanks,

    Saroj

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans