3

I work in a compliance office and I convinced my boss to start using SharePoint services and part of my job is to build a compliance tracker with dates etc.

I found a good solution here and after some adjustments I got most functions working nicely, except for the dates formatting due to the code being adapted for US dates, but we use UK date formatting.

The following bit of code is for US dates.

My question is: how to amend it so that it works with UK dates?

var CreateDate = new Date(ctx.CurrentItem.CreateDate.split('/')[2],ctx.CurrentItem.CreateDate.split('/')[0]-1,ctx.CurrentItem.CreateDate.split('/')[1]).getTime();

Perhaps I need to change other bits of the code, and I tried looking online, but I can't get it to work.

I'd appreciate your feedback, alternatively, if you know a better solution, please let me know.

Thanks in advance!

1

Handling Dates as Strings is a recipe for a lot of headaches

If you are going to work a lot with Dates then use the MomentJS library


If you go to the MomentJS site, the library will be available

and F12 Console commands give:

  • Thank for your alternative. I will probably have to spend some time learning moment.js and how to implement it, but for now, is there a simple way to fix the above code, so that it work with UK dates format? – Maciek May 5 '17 at 10:37
  • That code will be more complex (because you have to take edge cases into account) than MomentJS – Danny '365CSI' Engelman May 5 '17 at 13:30
  • Oh I see. Would you be able to tell me what I need to do with this bit of code the get moment.js involved? I uploaded moment.js to my site, but I have no idea how to implement it :( I was reading a lot on moment's website, but I still have a lot to learn... – Maciek May 5 '17 at 13:33
  • read the momentJS documentation. You first have to learn JavaScript before you can program Javascript. I fear the other way around doesn't really work... you can't run before you can walk – Danny '365CSI' Engelman May 5 '17 at 13:36
  • The code above doesn't work even if I change locale to US. Knowing JS is one thing, but getting it to work with SharePoint is another. Do you think the code is otherwise fine? Why is it not working with US locale? – Maciek May 5 '17 at 13:49

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.