How to convert date format in json response to string format in sharepoint?
-
I have set a column to column type "Date" and the json response from listData.svc for this column is in this format "Created": "/Date(1377683175000)/" , can someone suggest how to convert this format to string? I have tried new Date(Time).toLocaleString (Time is the variable that hold the date/time field) but doesn't work.
-
Answer:
If you are developing javascript for SharePoint and can use its javascript libraries then it might be a good idea to be using standart approach which take care of all the rest var date = new Date(); var arg = { date: date }; var str = Sys.Serialization.JavaScriptSerializer.serialize(arg);// "{"date":"\/Date(1377757755979)\/"}" var newObject = Sys.Serialization.JavaScriptSerializer.deserialize(str); var newDate = newObject.date; // Date {Thu Aug 29 2013 10:29:15 GMT+0400} The class is a part of http://msdn.microsoft.com/en-us/magazine/cc163300.aspx so it is a good idea to use it whenever you want to communicate with standart Microsoft Web Services.
user1758952 at SharePoint Visit the source
Other answers
private static DateTime ConvertJsonTimestamp(double jsonTime) { var baseTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); return baseTime.AddSeconds(jsonTime); } pass in your Time variable to this method and you will get a .net date back. There are other methods, but most requires som third party library like Newtonsoft
Robert Lindgren
As part of .Net Framework at least two options are available: Deserialize method from http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx ReadObject method from http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx Example string dateString = @"""\/Date(1377683175000)\/"""; var serializer = new JavaScriptSerializer(); var date = serializer.Deserialize<DateTime>(dateString); //returns {28.8.2013 9:46:15}
Vadim Gremyachev
Related Q & A:
- How to convert an entity to Json?Best solution by Stack Overflow
- How to convert Json to CSV or Excel?Best solution by Stack Overflow
- How to parse inner array in JSON response with PHP?Best solution by Stack Overflow
- How to convert xml to json in c#?Best solution by Stack Overflow
- How to change date format using jquery?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.