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 :

JavaScript: Working with Dates Returned from Web Service Calls

Mitch Milam Profile Picture Mitch Milam

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.

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

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

 

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


This was originally posted here.

Comments

*This post is locked for comments