19

I am getting date in 2015-10-30T05:00:00Z format from a SharePoint list using rest. Please help me to get the date in 2015-10-30 format using JavaScript.

KumarV
  • 2,680
  • 12
  • 53
  • 98

4 Answers4

29

SharePoint adds a String.format function
so you can use:

String.format('{0:yyyy}-{0:MM}-{0:dd}',new Date('2015-10-30T05:00:00Z'));

Note: String.format is defined in msajaxbundle.js, loaded even before most of the JavaScript files so safe to use without SOD requirements or anything.
It was modelled after the C# and VB implementations, so the MSDN documentation applies
(for the major part; it does not do the alignment stuff as that makes no sense in HTML)

String.format("{0:i}",new Date());  outputs: Wed Oct 07 2015 20:39:54 GMT+0200 (W. Europe Daylight Time)
String.format("{0:F}",new Date());  outputs: Wednesday, 07 October 2015 20:39:54
String.format("{0:f}",new Date());  outputs: Wednesday, 07 October 2015 20:39
String.format("{0:D}",new Date());  outputs: Wednesday, 07 October 2015
String.format("{0:s}",new Date());  outputs: 2015-10-07T20:39:54
String.format("{0:d}",new Date());  outputs: 10/07/2015
String.format("{0:dd}",new Date());  outputs: 07
String.format("{0:ddd}",new Date());  outputs: Wed
String.format("{0:dddd}",new Date());  outputs: Wednesday
String.format("{0:m}",new Date());  outputs: October 07
String.format("{0:M}",new Date());  outputs: October 07
String.format("{0:MM}",new Date());  outputs: 10
String.format("{0:MMM}",new Date());  outputs: Oct
String.format("{0:MMMM}",new Date());  outputs: October
String.format("{0:Y}",new Date());  outputs: 2015 October
String.format("{0:y}",new Date());  outputs: 2015 October
String.format("{0:yy}",new Date());  outputs: 15
String.format("{0:yyyy}",new Date());  outputs: 2015
String.format("{0:gg}",new Date());  outputs: A.D.
String.format("{0:T}",new Date());  outputs: 20:39:54
String.format("{0:t}",new Date());  outputs: 20:39
String.format("{0:HH}",new Date());  outputs: 20
String.format("{0:mm}",new Date());  outputs: 39
String.format("{0:ss}",new Date());  outputs: 54

It does more then just Dates:

MSDN String.format() Documentation

J5 iJS string format top20 iDate

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79
6

I would strongly suggest using the MomentJS library for all date/time work client side. Writing JavaScript functions to handle this stuff is so 2000s at this point. I load MomentJS into every client side project I do these days.

http://momentjs.com

Marc D Anderson
  • 9,686
  • 2
  • 36
  • 51
  • 1
    I agree with MomentJS for anything but a few date operations. Also note SharePoint has a sp.datetimeutils.js library with some (obscure) functions; but GetDaysAfterToday() is a real goodie – Danny '365CSI' Engelman Oct 29 '15 at 23:33
  • As always it depends what you're trying to do, momentjs is 12.4k gzipped, if you're displaying one date in ISO format with the time chopped off it's probably overkill. – John-M Oct 30 '15 at 13:12
2

Store the value retrieved from REST call in a variable and use JavaScript slice method to get the date part. Example:

var x = "2015-10-30T05:00:00Z";
var y = x.slice(0,10);
alert(y);
Nadeem Yousuf-AIS
  • 18,707
  • 4
  • 28
  • 59
2

If you're in a provider hosted app (or working with non .NET folks) you may want to use plain old Javascript; here is a technique that doesn't rely on string manipulation, if you're worried about that:

function datetoISODate(dateObj) {
    return dateObj.getFullYear() + "-" + (dateObj.getMonth() + 1) + "-" + dateObj.getDate();
}

Then you can pass in a new Date object created with your JSON return values, calling the function like datetoISODate(new Date("2015-10-30T05:00:00Z"))

Note that Date.prototype.getMonth is 0 based, (January => 0, February =>1, ...) so you need to add 1 back to get the format you're expecting.

John-M
  • 5,930
  • 3
  • 18
  • 36