Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Set date field using javascript

(0) ShareShare
ReportReport
Posted on by 240

Hi All,

I am receiving date in 18-Aug-2015 format.

I need to set this to one of the date field.

I tried with below code.

var dateData= new Date("18-Aug-2015");
Xrm.Page.data.entity.attributes.get("aaa_definedDate").setValue(dateData);
Xrm.Page.data.entity.attributes.get("aaa_definedDate").setSubmitMode("always");

*This post is locked for comments

  • NK Bhairi Profile Picture
    NK Bhairi 240 on at
    RE: Set date field using javascript

    4540.Capture.PNG

    actualDate= 01/30/2019;

    var dateData = new Date(actualDate);

    Xrm.Page.data.entity.attributes.get("ab_setdate").setValue(dateData);

    This is working fine in chrome.

    But it is throwing date error in IE and Edge.

    Error Message: String was not recognized as a valid DateTime.

  • NK Bhairi Profile Picture
    NK Bhairi 240 on at
    RE: Set date field using javascript

    047321.Capture.PNG

    When using 

    var dateData = new Date(actualDate);

    Xrm.Page.data.entity.attributes.get("aaa_inspected").setValue(dateData);

    this is working fine in chrome browser and setting the date to the correct value.

    But in IE and edge it is populating like above. when i am saving the record it is throwing me date error because of the leading zeros.

  • Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Set date field using javascript

    Hi,

    would you mind to share some pictures?

  • NK Bhairi Profile Picture
    NK Bhairi 240 on at
    RE: Set date field using javascript

    sorry I am receiving Date in 28-Aug-18

  • LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: Set date field using javascript

    Hi partner,

    You should set JavaScript date object to datetime field.

    You could try :

    var date1=new Date();

    Xrm.Page.data.entity.attributes.get("aaa_definedDate").setValue(date1);

    And then check if it works.

    I have tried in my instance and it worked well.

    You could also refer to the following links.

    community.dynamics.com/.../194113

    bernado-nguyen-hoan.com/.../amp

    Hope it helps.

    Best Regards,

    Leo

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Set date field using javascript

    Hi,

    Can you please tell me the issue. I tried the same code which is mentioned above and its working absolutely fine.

    Thanks,

    Shivaram.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Set date field using javascript

    Hi,

    I am testing with below code. It worked without any problems.

    function setDate()

    {

    var d = new Date("18-Aug-2015");

    Xrm.Page.getAttribute("new_finaldate").setValue(d);  // remember to change the schema name

    Xrm.Page.getAttribute("new_finaldate").setSubmitMode("always"); // remember to change the schema name

    }

    2043.3.PNG

  • gdas Profile Picture
    gdas 50,085 on at
    RE: Set date field using javascript

    It's should work ,try to debug and share error details .

  • NK Bhairi Profile Picture
    NK Bhairi 240 on at
    RE: Set date field using javascript

    no luck

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Set date field using javascript

    Hi ,

    Try with this  -

         function getdatetime() {
                var strDt = "18-Aug-2015"; // Set date value 
                var dt = strDt.split('-');
                var day = dt[0];
                var month = dt[1].toLocaleLowerCase();
                var year = dt[2];
                if (month == "jan")
                    month = 1;
                else if (month == "feb")
                    month = 2;
                else if (month == "mar")
                    month = 3;
                else if (month == "apr")
                    month = 4;
                else if (month == "may")
                    month = 5;
                else if (month == "jun")
                    month = 6;
                else if (month == "jul")
                    month = 7;
                else if (month == "aug")
                    month = 8;
                else if (month == "sep")
                    month = 9;
                else if (month == "oct")
                    month = 10;
                else if (month == "nov")
                    month = 11;
                else if (month == "dec")
                    month = 12;
    
                var actualDate = month + "/" + day + "/" + year;
                var dateData = new Date(actualDate);
                Xrm.Page.data.entity.attributes.get("aaa_definedDate").setValue(dateData);
                Xrm.Page.data.entity.attributes.get("aaa_definedDate").setSubmitMode("always");
            }


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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans