Thursday, February 2, 2012

DateTime Format While Retrieving Using oData

Problem:
            While retrieving date attribute using oData call, the format of date is not correct , need to format data value so that it can be user friendly. As you can see in the Image 1.

Image 1
Solution:
            The solution of the above problem is listed in List 1. Here you can see that I have taken this code from my post "Insert New Record In Micosoft CRM 2011 Using oData Call ", where I have show you how to insert new record by using oData call and you can see in Image 2 of the last post date is not correctly formatted. Here in the code I have modified the completed callback of the Account create async call of my post "Insert New Record In Micosoft CRM 2011 Using oData Call .


List 1

You can see in above code that I have first store CreatedOn value in the stringDataValue object by using eval and converting to string so that I can perform string function on that. In the next statement I have used the Date constructor to create the date, In the Data constructor I have used the parseInt function of JavaScript to parse the value which is return by using the replace function of the string, then I have replace the unwanted characters from the data value like "/Data(" and ")/" so that I have the exact integer value of the date.You can see the output of the result in Image 2



That is how you can format the date when you use the odata call to get the date attribute of the entity. As you can see that the output also show you the time zone as well here it will show +5 for my time zone you can use this as well if you have different time value to shown based on your requirements.  

All and any comments / bugs / suggestions are welcomed!