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)

Set date field using javascript

(0) ShareShare
ReportReport
Posted on by 244

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

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

    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");
            }


  • NK Bhairi Profile Picture
    244 on at

    no luck

  • gdas Profile Picture
    50,091 Moderator on at

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

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    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

  • shivaram Profile Picture
    3,315 on at

    Hi,

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

    Thanks,

    Shivaram.

  • LeoAlt Profile Picture
    16,331 Moderator on at

    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

  • NK Bhairi Profile Picture
    244 on at

    sorry I am receiving Date in 28-Aug-18

  • Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    would you mind to share some pictures?

  • NK Bhairi Profile Picture
    244 on at

    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.

  • NK Bhairi Profile Picture
    244 on at

    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.

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