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?

Mark L
  • 4,065
  • 7
  • 66
  • 128

3 Answers3

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());
Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79
  • 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 02:12
-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

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Dipen Shah
  • 1,128
  • 9
  • 19
-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);
Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Dipen Shah
  • 1,128
  • 9
  • 19