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

1

2 Answers 2

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

3
  • Your suggestion does not work in javascript object model on IE 8. Commented Aug 19, 2013 at 14:00
  • 2
    It works on my computer, but what about specifying the culture? Commented Jun 11, 2014 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! Commented Oct 16, 2017 at 16:58
1

I like to use Moment.js

It is easy to learn and very small.

2
  • 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. Commented Aug 19, 2013 at 15:54
  • Moment.js is deprecated Commented Jun 28, 2022 at 7:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.