Sign up ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
Do you need only 2015-10-30? Why would you want 10th as well as 2015-10-30 – Nadeem Yousuf yesterday
    
I just need 2015-10-30 this format. – vikash kumar yesterday

4 Answers 4

up vote 10 down vote accepted

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 ScriptResource.axd, 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)

share|improve this answer

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

share|improve this answer

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);
share|improve this answer
2  
Absolute positioning and String operations on Dates.. Never a good match. If a system outputs january 1st as 2016-1-1T05:00:00Z ..... – Danny Engelman yesterday
    
I believe the date retrieved from SharePoint Date fields will always be in the form mm and dd. – Nadeem Yousuf yesterday
    
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 pittfalls – Danny Engelman yesterday
    
I agree on that. Your way of doing it is a clean way. – Nadeem Yousuf yesterday

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.