1

I am using SP2013 on-premise. I have applied client side rendering to convert the Item created datetime to specific format.

The problem is "ctx.CurrentItem.Created" always return a string format as below

2015/10/20 PM 06:09

in my browser. Our farm have Chinese language pack installed and my locale is Hong Kong SAR. It maybe the reason why AM/PM is sitting before the time.

On IE11, when I run JS like below:

var d = new Date(ctx.CurrentItem.Created);

it works. However it is not supported in Chrome. Chrome cannot handle the "AM/PM" sitting before the time.

My question is, can I change the format of ctx.CurrentItem.Created to some ISO format?

  • I am working on it, i ll get back to u . – Dipen Shah Jan 12 '16 at 5:23
  • I am using momentjs.com to get rid of all those date problems. Saved me lot of time! – Patrick Jan 12 '16 at 7:12
4

Someone suggested the MomentJS library, which is great for doing Date calculations and transformations.
But 2015/10/20 PM 06:09 is an invalid Date notation so MomemtJS won't make it valid either.

I would suggest to find the cause why your environment is outputting this invalid notation and fix it at the source.

In the mean time you can change the string with:

    var datestring="2015/10/20 PM 06:09";
    var dateparts=datestring.split(' ');//array
    if(dateparts[1].length===2){//am or pm in second array element?
      dateparts.push(dateparts[1]);//move it to the end
      dateparts.splice(1,1);//take out the am/pm in the middle
    }
    var mydate=dateparts.join(' ');//make it one string
    console.log(mydate);
    console.log(new Date(mydate).toString());
    console.log(new Date(mydate).toISOString());
|improve this answer|||||
  • Thanks your solution will work. I am quite sure it is the problem of language pack as we have two farms have identical output. We got various problem on language pack. – Mark L Jan 13 '16 at 2:12
-1

You should change your String date format

I have tried with 2015/10/20 06:09 PM

var t = new Date("2015/10/20 06:09 PM");
t = t.toISOString()
alert(t);
|improve this answer|||||
  • I don't know how to change the string format returned by ctx.CurrentItem.Created – Mark L Jan 12 '16 at 6:50
  • ctx.currentItem.Cretaed give you "20/10/2015 06:09 PM" Format. and you have to parse in javascript – Dipen Shah Jan 12 '16 at 9:00
-1

Using below code you can get ISO format of SharePoint created field value

  string objTime=SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime("2015/10/20 PM 06:09"));

Output:

2015-10-20T18:09:00Z

|improve this answer|||||
  • 1
    Nice Server Side Solution! But the question is regarding CSR – Atish Dipongkor - MVP Jan 12 '16 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.