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.

2
  • Do you need only 2015-10-30? Why would you want 10th as well as 2015-10-30CommentedOct 28, 2015 at 8:27
  • I just need 2015-10-30 this format.
    – KumarV
    CommentedOct 28, 2015 at 8:31

4 Answers 4

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

    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

    2
    • 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 goodieCommentedOct 29, 2015 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
      CommentedOct 30, 2015 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); 
    4
    • 2
      Absolute positioning and String operations on Dates.. Never a good match. If a system outputs january 1st as 2016-1-1T05:00:00Z .....CommentedOct 28, 2015 at 8:52
    • 1
      I believe the date retrieved from SharePoint Date fields will always be in the form mm and dd.CommentedOct 28, 2015 at 8:54
    • Sure.. SharePoint does.. but what happens if a future Developer (its allways humans causing problems) rewrites part of the script and expects Dates are handled as Dates. Nothing wrong with treating Dates as Strings... as long as one is aware of potential pittfallsCommentedOct 28, 2015 at 9:11
    • 1
      I agree on that. Your way of doing it is a clean way.CommentedOct 28, 2015 at 9:21
    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.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.