locked
CRM 2013 - JScript and Rest End Point - Date time issue RRS feed

  • Question

  • I'm trying to lookup a datetime field using the rest/odata query.

                    var value = null;
                    if (retrievedValue.new_ReceivedDate != null) {
                        value = retrievedValue.new_ReceivedDate;
                        alert(retrievedValue.new_ReceivedDate);
                        //alert(value);
                        var newdate = xmlDthToDate(value);
                        //var newdate = value;
                        alert(newdate);
                        Xrm.Page.getAttribute("new_complaintreceiptdate").setValue(newdate);
                        Xrm.Page.getAttribute("new_complaintreceiptdate").setSubmitMode("always");
                    }

    It currently brings back this format from the query, how do I now use this to set a datetime field? I used to have a function that did it in CRM 2011 but after upgrading to 2013 it doesn't work.

    Any advice appreciated.

    Thanks


    Pete

    Tuesday, January 7, 2014 10:46 AM

All replies

  • try this! may it work for you
    var startedDate = new Date(parseInt(startondate.replace("/Date(", "").replace(")/", ""), 10));
    var startedrawMonth=(startedDate.getMonth()+1).toString();
    var startedrawDate=startedDate.getDate();
    var startedyear=startedDate.getFullYear();
    var StartedDate1=startedyear+"-"+startedrawMonth+"-"+startedrawDate;


    ms crm

    Tuesday, January 7, 2014 11:03 AM
  • Hi,

    You can use a function similar to:

     function (value) {
      var a;
      if (typeof value === 'string') {
       a = /Date\(([-+]?\d+)\)/.exec(value);
       if (a) {
        return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
       }
      }
      return value;
     }

    Hope this helps,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Tuesday, January 7, 2014 11:09 AM
    Answerer
  • Hi,
    Reddy A answer is correct, however after you convert the date from the unix time (returned by your REST call) you can set directly to a datetime field:

    var convertedDate = new Date(parseInt(newdate.replace("/Date(", "").replace(")/", "")));
    Xrm.Page.getAttribute("new_complaintreceiptdate").setValue(convertedDate); 



    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, January 7, 2014 11:11 AM
  • Cheers guys, had a bit of a play and I needed to parse the month/day as 2 digits so got this working in the end with

                        var startedDate = new Date(parseInt(startondate.replace("/Date(", "").replace(")/", ""), 10));
                        var startedrawMonth = (("0" + (startedDate.getMonth() + 1)).slice(-2)).toString();
                        var startedrawDate = (("0" + (startedDate.getDate())).slice(-2)).toString();     
                        var startedyear = startedDate.getFullYear();
                        var startedDate1 = startedyear + "-" + startedrawMonth + "-" + startedrawDate;
    
                        var newdate = new Date(startedDate1)


    Pete

    Tuesday, January 7, 2014 12:00 PM