locked
How to convert Javascript var to CrmDateTime in Javascript RRS feed

  • General discussion

  • Hi,

    How to convert Javascript var to CrmDateTime in Javascript.

    Ex:-

    var remsactdate = crmForm.all.us_remsactivitysentdelivered.DataValue;

    then how to convert this remsactdate var to Crmdatetime.

    Please provide me some sample code.

    Thanks,

    Naveen


    Naveen MSCRM Technical Consultant
    Wednesday, November 10, 2010 4:57 PM

All replies

  • you can set value use follow code:

    crmForm.all.us_remsactivitysentdelivered.DataValue=new Date();


    jeff.han
    Wednesday, November 10, 2010 11:40 PM
  • you can directly assign var to CrmDateTime: Here is the code for it.

    // If you are getting CRM value here

    var remsactdate = crmForm.all.us_remsactivitysentdelivered.DataValue;

     

    // You can assign the same var value to crm dateTime.

    crmForm.all.us_remsactivitysentdelivered.DataValue= remsactdate;


    Jai Ho CRM http://mscrmkb.blogspot.com Skype - amol.gholap

    Mark as answer if a post has answered the question
    Thursday, November 11, 2010 4:55 AM
  • Hi,

    Actually in my scenario i need to get the Crmdatetime value in Javascript and pass this value to RetrieveMultile method.

     

    my code as below:


    var remsactdate = crmForm.all.us_remsactivitysentdelivered.DataValue;

    Now i need to pass this remsactdate var to RetrieveMultile method.

    If i am directly passing this, getting the date format issue. 

     

    var authenticationHeader = GenerateAuthenticationHeader();

    // Prepare the SOAP message.

    var xml = "<?xml version='1.0' encoding='utf-8'?>"+ 

    "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+

    " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+

    " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 

    authenticationHeader+ 

    "<soap:Body>"+ 

    "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 

    "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+

    " xsi:type='q1:QueryExpression'>"+ 

    "<q1:EntityName>appointment</q1:EntityName>"+ 

    "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+ 

    "<q1:Attributes>"+ 

    "<q1:Attribute>us_name</q1:Attribute>"+ 

    "</q1:Attributes>"+ 

    "</q1:ColumnSet>"+ 

    "<q1:Distinct>false</q1:Distinct>"+ 

    "<q1:Criteria>"+ 

    "<q1:FilterOperator>And</q1:FilterOperator>"+ 

    "<q1:Conditions>"+ 

    "<q1:Condition>"+ 

    "<q1:AttributeName>regardingobjectid</q1:AttributeName>"+ 

    "<q1:Operator>Like</q1:Operator>"+ 

    "<q1:Values>"+ 

    "<q1:Value xsi:type='xsd:string'>"+physicianid+"</q1:Value>"+ 

    "</q1:Values>"+ 

    "</q1:Condition>"+ 

    "<q1:Condition>"+ 

    "<q1:AttributeName>scheduledstart</q1:AttributeName>"+ 

    "<q1:Operator>Like</q1:Operator>"+ 

    "<q1:Values>"+ 

    "<q1:Value xsi:type='xsd:string'>"+remsactdate+"</q1:Value>"+ // Here I need to give the correct                                                                                                            date format

    "</q1:Values>"+ 

    "</q1:Condition>"+ 

    "</q1:Conditions>"+ 

    "</q1:Criteria>"+

    "</query>"+ 

    "</RetrieveMultiple>"+ 

    "</soap:Body>"+ 

    "</soap:Envelope>";

    // Prepare the xmlHttpObject and send the request.

    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");

    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

    xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");

    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xHReq.setRequestHeader("Content-Length", xml.length);

    xHReq.send(xml);

    // Capture the result.

    var resultXml = xHReq.responseXML;

    //var resultXml = xHReq.responseXML.selectNodes("//BusinessEntity/q1:name");

     

    // Check for errors.

    var errorCount = resultXml.selectNodes('//error').length;

    if (errorCount != 0)

    {

     var msg = resultXml.selectSingleNode('//description').nodeTypedValue;

     alert(msg);

    }

    // Parse and display the results.

    else

    {

     var results = resultXml.getElementsByTagName('BusinessEntity');

     //var Totalresult =1;

     //Totalresult += results.length;

     var msg = "";

     if (results.length == 0)

     {

     msg = "No Patient Profile were found with status "+StatusValue+".";

     alert(msg);

     return;

     }

     else

     {var msg="Total Count="+results.length;

     alert(msg);

     }

     

    Please provide me sample code how we can convert this Javascript var to CrmDateTime value.

     

    Thanks,

    Naveen

     


    Naveen MSCRM Technical Consultant
    Thursday, November 11, 2010 8:16 AM
  • You can use the below function

    Date.prototype.toFormattedString = function(format)
    {
       var d = this;
       var f = "";
       f = f + format.replace( /dd|mm|yyyy|MM|hh|ss|ms|APM|\s|\/|\-|,|\./ig ,
       function match()
       {
          switch(arguments[0])
          {
             case "dd":
                var dd = d.getDate();
                return (dd < 10)? "0" + dd : dd;
             case "mm":
                var mm = d.getMonth() + 1;
                return (mm < 10)? "0" + mm : mm;
             case "yyyy": return d.getFullYear();
             case "hh":
                var hh = d.getHours();
                return (hh < 10)? "0" + hh : hh;
             case "MM":
                var MM = d.getMinutes();
                return (MM < 10)? "0" + MM : MM;
             case "ss":
                var ss = d.getSeconds();
                return (ss < 10)? "0" + ss : ss;
             case "ms": return d.getMilliseconds();
             case "APM":
                var apm = d.getHours();
                return (apm < 12)? "AM" : "PM";
             default: return arguments[0];
          }
       });

       return f;
    }

    function OnCrmPageLoad()
    {
        var d = new Date(crmForm.all.<DATETIME Field>.DataValue);
        alert(d.toFormattedString("mm-dd-yyyy hh:MM:ss ms"));
        alert(d.toFormattedString("dd/mm/yyyy hh:MM APM"));
    }

    OnCrmPageLoad();

    refer here for more information.

    Thursday, November 11, 2010 8:53 AM
  • Try:

    FormatUtcDate(new Date(crmForm.all.us_remsactivitysentdelivered.DataValue))


    Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)
    Thursday, November 11, 2010 9:39 AM