GetListItems 'Created' date format

Dec 13, 2011 at 3:50 PM

Hi,

I am using the GetListItems operation.  I am pulling from the column "Created".  

Right now, my output looks like this:  2011-12-07 16:29:16

How can i make it look like this:  12-07-2012 4:29pm 

Thanks!

Coordinator
Dec 13, 2011 at 3:54 PM
Edited Dec 13, 2011 at 3:55 PM

You can use substring functions to reformat your date and time column values.

M.

Dec 13, 2011 at 4:05 PM

Could you give me an example please?  

Coordinator
Dec 13, 2011 at 4:18 PM
Edited Dec 13, 2011 at 4:30 PM

You should be able to find plenty of examples for date/time formatting on the Web. There are even libraries that can help you.

M.

Dec 15, 2011 at 5:48 PM

Here's a quick and dirty function I put together that will return a string formatted the way you want it:

function SPConvertDate(t){
     var d = new Date(t.substr(0,4), parseInt(t.substr(5,2)) - 1, t.substr(8,2), t.substr(11,2), t.substr(14,2), t.substr(17,2));

     var month = ((d.getMonth()+1) < 10) ? "0" + (d.getMonth()+1) : (d.getMonth()+1);
     var date = (d.getDate() < 10) ? "0" + d.getDate() : d.getDate();
     var minutes = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes();
     var amPm = (d.getHours() < 12) ? "am" : "pm";

     return month + "-" + date + "-" + d.getFullYear() + " " + d.getHours() + ":" + minutes + amPm;
}

This converts the response you receive from SPServices into a javascript Date object, then uses built-in javascript methods to parse that Date and return a string formatted the way you want it.

You can call it from SPServices in the completefunc callback like this:

$("#createdDateContainer").html( SPConvertDate($(this).attr("ows_Created")) );

Hopefully looking through that function will help you understand a little bit more about how dates work in JS.

Dec 18, 2011 at 10:10 AM

This library helped me a lot with all the date format issues