8

I am using the javascript object model to get a date field back and display it on a page e.g. oListItem.get_item("MyTestDate")

This displays on the page by default as: Thu Jul 25 09:30:07 PDT 2013

I want to display it as: 25 Jul 2013, 09:30

How do i format a date when using the JSOM

14

ASP.NET AJAX (which is a part of SharePoint - MicrosoftAjax.js) extends the JavaScript Date object with a formatting function that closely mimics the DateTime.ToString method

So, in your case the format string should be as follows: dd MMM yyyy, hh:ss

var modified = item.get_item("Modified");
var fmtModified = modified.format('dd MMM yyyy, hh:ss');

For a full list of formatting string parameters, see the Standard DateTime Format Strings and Custom DateTime Format Strings reference pages on MSDN

  • Your suggestion does not work in javascript object model on IE 8. – Mario Pereira Aug 19 '13 at 14:00
  • 2
    It works on my computer, but what about specifying the culture? – Steve B Jun 11 '14 at 13:41
  • 1
    Vadim, extremely helphul! I've sp[ent few hours oO getRelativeDateTimeString didn't work for me, it does not include time.. You saved my time! Thank You much! – Gennady G Oct 16 '17 at 16:58
1

I like to use Moment.js

It is easy to learn and very small.

  • 1
    I was about to suggest this myself. Moment.js is an excellent library. I use it in all of my SharePoint projects that have custom front-end work. – Robert Kaucher Aug 19 '13 at 15:54

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.