Knowledge found and lost while working with Microsoft Dynamics CRM
RSS icon Home icon
  • JavaScript: Working with Dates Returned from Web Service Calls

    Posted on June 10th, 2009 mitch Print Print 2 comments

    Revised Thursday, July 11th.

    I’ve been doing a lot of work recently using JavaScript to retrieve data from CRM via web service calls.  One of the issues I ran into is the date format returned by CRM is in Coordinated Universal Time ( UTC ) which is not the same format most programming languages use.  This leads to conversion issues or programming exceptions.

    To work around any issues, I created a JavaScript function that allows me to parse the UTC date and time string and return a JavaScript Date value.

    At this point, the function is only concerned with the date, but if you need the time, you can modify the function to add those additional parameters to the new Date() line.

    For more information on the JavaScript Date object, please visit the following link.

    function ConvertDateFromUTC(startDate){
    // UTC time formatted string
    //2009-03-25T09:30:00-00:00 // 1 2 //1234567890123456789012345 
     
    var year = parseInt(startDate.substr(0, 4),10);
    var month = parseInt(startDate.substr(5, 2),10);
    var day = parseInt(startDate.substr(8, 2),10);
    var hour = parseInt(startDate.substr(11, 2),10);
    var min = parseInt(startDate.substr(14, 2),10); 
     
    return new Date(year, month - 1, day);
    }

    July 11th: Corrected issues as pointed out by Moti, in the comments.

     

    Making It Work

    In this example, I’m using the excellent CrmService library released by Ascentium, to retrieve values from a contact.

    var oService = new Ascentium_CrmService();
     
    var asCols = ["fullname", 
                  "address1_postalcode", 
                  "address1_city", 
                  "address1_stateorprovince", 
                  "birthdate"];
     
    var beRetrievedContact = 
           oService.Retrieve("contact", 
           crmForm.all.customerid.DataValue[0].id, 
           asCols);
     
    var birthDateString = beRetrievedContact.attributes["birthdate"].value;
     
    var birthDate = new Date(ConvertDateFromUTC(birthDateString));

    Customization, Dynamics CRM
    1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
    882 views
     

    2 responses to “JavaScript: Working with Dates Returned from Web Service Calls”

    1. Your code is buggy. For example, it would not work for dates in August or Septemeber.

      To fix it, you need to use the radix argument for the parseInt method. Otherwise, any string input starting with a 0, will be treated as an octal base number.

      Here is the fix:
      function ConvertDateFromUTC(startDate){
      // UTC time formatted string
      //2009-03-25T09:30:00-00:00 // 1 2 //1234567890123456789012345

      var year = parseInt(startDate.substr(0, 4),10);
      var month = parseInt(startDate.substr(5, 2),10);
      var day = parseInt(startDate.substr(8, 2),10);
      var hour = parseInt(startDate.substr(11, 2),10);
      var min = parseInt(startDate.substr(14, 2),10);

      return new Date(year, month – 1, day);
      }

    2. Thanks Moti. Revised code has been noted.

    Leave a reply